开发者

Deleting some nodes from an XML document when iterating using XSLT

I'm having a little issue with some XSLT.

My original XML looks like this:

<?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>

My XSLT looks like this:

<?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> 

...and my output looks like this:

<?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>

I need to remove the following tags from my output \rowset\row\transaction\comment, \rowset\row\transaction\client_idand \rowset\row\transcation\currency. Although I've managed to twist the XML to almost how I want it, I can't seem to remove the nodes I don't want.

In the original XML there can be more than one transaction in transaction_set and each transaction can contain multiple comment. I'm trying to concatenate all the comment\comment_text records which I've managed to do but I need these comment tags removed from \rowset\row\transaction in the output XML.

Maybe I'm tackling this in the wrong way but I can't get my head around it.


An easier way to approach this is to take an identity transform (i.e. one which simply copies each element / attribute), then add no-op matches for the elements you wish to omit:

<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>

See this related question:

XSL Transform remove Xml Elements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜