开发者

Sorting only specific XML nodes

I need to sort only the <Transaction-Detail> nodes (by the <tran-id> child node) of the following file:

<TransActDO clear="true" removed="false">
  <stmt-reason-code>1001</stmt-reason-code>
  <Transaction-Detail clear="true" removed="false">
    <txn-amt>788.20</txn-amt>
    <txn-description>New Purchase</txn-description>
    <tran-id>3271</tran-id>
  </Transaction-Detail>
  <Transaction-Detail clear="true" removed="false">
    <txn-amt>-68.20</txn-amt>
    <txn-description>Return</txn-description>
    <tran-id>27795</tran-id>
  </Transaction-Detail>
  <Transaction-Detail clear="true" removed="false">
    <txn-amt>0.00</txn-amt>
    <txn-description>Comment</txn-description>
    <transact开发者_如何学JAVAion-reason-desc>No Reason</transaction-reason-desc>
    <tran-id>13365</tran-id>
    <transaction-reason-code>0</transaction-reason-code>
  </Transaction-Detail>
  <Transaction-Detail clear="true" removed="false">
    <txn-amt>343.45</txn-amt>
    <txn-description>New Purchase</txn-description>
    <tran-id>7558</tran-id>
  </Transaction-Detail>
  <Transaction-Detail clear="true" removed="false">
    <txn-amt>0.00</txn-amt>
    <txn-description>Comment</txn-description>
    <transaction-reason-desc>No Reason</transaction-reason-desc>
    <tran-id>6512</tran-id>
    <transaction-reason-code>0</transaction-reason-code>
  </Transaction-Detail>
  <account-no>123456789</account-no>
  <payer-name>JOHN DOE</payer-name>
  <Product-Detail clear="true" removed="false">
    <Name>WIDGET</Name>
    <Amount>89.00</Amount>
  </Product-Detail>
  <Product-Detail clear="true" removed="false">
    <Name>NEWER WIDGET</Name>
    <Amount>99.99</Amount>
  </Product-Detail>
  <stmt-reason-desc>Web Statement</stmt-reason-desc>
  <type>Original</type>
</TransActDO>

The output is XML and needs to also copy all other nodes and attributes from the original file. In essence, copy everything, just sort the Transaction-Detail nodes.

I've got this far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="TransActDO">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="Transaction-Detail">
        <xsl:sort select="tran-id" data-type="number" order="ascending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

This results in a properly sorted XML file containing only the Transaction-Detail nodes and their child nodes. When ever I attempt to add additional logic to copy the rest of the nodes, the sort breaks.

I guess I'm having difficulty wrapping my brain around XSLT execution theory and syntax.

Any help is much appreciated!

-nth-


This will do it:-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252"  />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="TransActDO">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(preceding-sibling::Transaction-Detail) and not(self::Transaction-Detail)]"/>
            <xsl:apply-templates select="Transaction-Detail">
                <xsl:sort select="tran-id" data-type="number" order="ascending"/>
            </xsl:apply-templates>
            <xsl:apply-templates select="@*|node()[not(following-sibling::Transaction-Detail) and not(self::Transaction-Detail)]"/>
    </xsl:copy>
    </xsl:template>


</xsl:stylesheet>

If you don't mind moving the Transaction-Detail elements to either the top or bottom of the TransActDO element you could simplify the inner set of apply templates to:-

            <xsl:apply-templates select="@*|node()[not(self::Transaction-Detail)]"/>
            <xsl:apply-templates select="Transaction-Detail">
                <xsl:sort select="tran-id" data-type="number" order="ascending"/>
            </xsl:apply-templates>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜