[英]Deleting some nodes from an XML document when iterating using XSLT
我对一些 XSLT 有一点问题。
我原来的 XML 看起来像这样:
<?xml version="1.0"?><rowset>
<row>
<trans_type>10</trans_type>
<creation_date>2011-06-07</creation_date>
<system_id>1039</system_id>
<transaction_set>
<transaction>
<trans_type>10</trans_type>
<client_id>977400</client_id>
<case_id>12881459</case_id>
<invoice_no>01/2011</invoice_no>
<payment_date>110606</payment_date>
<payment>710,08</payment>
<currency>EUR</currency>
<comment>
<record_type>612</record_type>
<comment_text>Asiakas maksoi lisäksi kesäkuun vuokran ajalle 1.6. - 15.6.2</comment_text>
</comment>
<comment>
<record_type>612</record_type>
<comment_text>011. Meillä saldo 0 €.</comment_text>
</comment>
</transaction>
</transaction_set>
<subtotal>
<trans_type>10</trans_type>
<count>25</count>
</subtotal>
</row>
</rowset>
我的 XSLT 看起来像这样:
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<rowset>
<xsl:for-each select="rowset/row/transaction_set/transaction">
<row>
<xsl:copy-of select="../../trans_type"/>
<xsl:copy-of select="../../creation_date"/>
<xsl:copy-of select="../../subtotal"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="./client_id"/>
<comment_text><xsl:for-each select="./comment"><xsl:value-of select="./comment_text"/></xsl:for-each></comment_text>
</row>
</xsl:for-each>
</rowset>
</xsl:template>
</xsl:stylesheet>
...我的 output 看起来像这样:
<?xml version='1.0' ?>
<rowset>
<row>
<trans_type>10</trans_type>
<creation_date>2011-06-07</creation_date>
<subtotal>
<trans_type>10</trans_type>
<count>25</count>
</subtotal>
<transaction>
<trans_type>10</trans_type>
<client_id>977400</client_id> <!--need this gone-->
<case_id>12881459</case_id>
<invoice_no>01/2011</invoice_no>
<payment_date>110606</payment_date>
<payment>710,08</payment>
<currency>EUR</currency> <!--need this gone-->
<comment> <!--need this gone-->
<record_type>612</record_type>
<comment_text>Asiakas maksoi lisäksi kesäkuun vuokran ajalle 1.6. - 15.6.2</comment_text>
</comment>
<comment> <!--need this gone-->
<record_type>612</record_type>
<comment_text>011. Meillä saldo 0 €.</comment_text>
</comment>
</transaction>
<client_id>977400</client_id>
<comment_text>Asiakas maksoi lisäksi kesäkuun vuokran ajalle 1.6. - 15.6.2011. Meillä saldo 0 €.</comment_text>
</row>
</rowset>
我需要从 output \rowset\row\transaction\comment
、 \rowset\row\transaction\client_id
和\rowset\row\transcation\currency
中删除以下标签。 虽然我已经设法将 XML 扭曲到几乎我想要的样子,但我似乎无法删除我不想要的节点。
在原始 XML 中, transaction_set
transaction
每个transaction
可以包含多个comment
。 我正在尝试连接我设法完成的所有comment\comment_text
记录,但我需要从 output XML 中的\rowset\row\transaction
中删除这些comment
标签。
也许我以错误的方式解决了这个问题,但我无法理解它。
解决此问题的一种更简单的方法是进行身份转换(即简单地复制每个元素/属性的转换),然后为您希望省略的元素添加无操作匹配:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- add elements that you want to omit here -->
<xsl:template match="//client_id"/>
<xsl:template match="//comment"/>
...
</xsl:stylesheet>
请参阅此相关问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.