使用xslt 1.0计算当前节点中的前趋节点

时间:2014-02-03 05:33:06

标签: xml xslt-1.0

我有一个输入xml,如

<Orders>
 <OrderId>123</OrderId>
 <Department>PO</Department>
 <OrderLines>
   <OrderLineId>234</OrderLineId>
   <Quantity>2</Quantity>
   <OrderLPN> --- 1st node
     <OrderLPN5Id>345</OrderLPNId>
     <OrderLPNDetail>
       <OrderLPNDetailId>456</OrderLPNDetailId>
       <sku>221</sku>
     </OrderLPNDetail>
     <OrderLPNDetail>
       <OrderLPNDetailId>457</OrderLPNDetailId>
       <sku>222</sku>
     </OrderLPNDetail>
    </OrderLPN>
   <OrderLPN> --- 2nd Node
     <OrderLPN5Id>346</OrderLPNId>
     <OrderLPNDetail>
       <OrderLPNDetailId>567</OrderLPNDetailId>
       <sku>333</sku>
     </OrderLPNDetail>
     <OrderLPNDetail>
       <OrderLPNDetailId>568</OrderLPNDetailId>
       <sku>367</sku>
     </OrderLPNDetail>
     <OrderLPNDetail>
       <OrderLPNDetailId>569</OrderLPNDetailId>
       <sku>368</sku>
     </OrderLPNDetail>
    </OrderLPN>
    <OrderLPN> --- 3rd Node
     <OrderLPN5Id>399</OrderLPNId>
     <OrderLPNDetail>
       <OrderLPNDetailId>845</OrderLPNDetailId>
       <sku>777</sku>
     </OrderLPNDetail>
     <OrderLPNDetail>
       <OrderLPNDetailId>846</OrderLPNDetailId>
       <sku>778</sku>
     </OrderLPNDetail>
    </OrderLPN>
 </OrderLines>
and so on....
</Orders>

目标xml也有4级别的层次结构。 现在,当我将LPN详细信息映射到Target xml时,我想计算当前节点中的前面的LPN详细信息节点。例如。

在循环(for-each)中,当我在LPN的第二个节点时,它应该给我     <seq> count(LPN Details[1st node of LPN])</seq> 当我在LPN的第三个节点时,它应该给我     <seq> count(LPN Details[1st node of LPN]) + count(LPN Details[2nd node of LPN])</seq>等等......

请建议。

1 个答案:

答案 0 :(得分:0)

此样式表

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <root>
            <xsl:for-each select="//OrderLPN">
                <seq>
                    <xsl:attribute name="id"><xsl:value-of select="OrderLPNId"/></xsl:attribute>
                    <xsl:value-of select="count(preceding::OrderLPN)"/>
                </seq>
            </xsl:for-each>
        </root>
    </xsl:template> 
</xsl:stylesheet>

应用于此输入

<?xml version="1.0" encoding="UTF-8"?>
<Orders>
    <OrderId>123</OrderId>
    <Department>PO</Department>
    <OrderLines>
        <OrderLineId>234</OrderLineId>
        <Quantity>2</Quantity>
        <OrderLPN><!--- 1st node -->
            <OrderLPNId>345</OrderLPNId>
            <OrderLPNDetail>
                <OrderLPNDetailId>456</OrderLPNDetailId>
                <sku>221</sku>
            </OrderLPNDetail>
            <OrderLPNDetail>
                <OrderLPNDetailId>457</OrderLPNDetailId>
                <sku>222</sku>
            </OrderLPNDetail>
        </OrderLPN>
        <OrderLPN><!--- 2nd Node -->
            <OrderLPNId>346</OrderLPNId>
            <OrderLPNDetail>
                <OrderLPNDetailId>567</OrderLPNDetailId>
                <sku>333</sku>
            </OrderLPNDetail>
            <OrderLPNDetail>
                <OrderLPNDetailId>568</OrderLPNDetailId>
                <sku>367</sku>
            </OrderLPNDetail>
            <OrderLPNDetail>
                <OrderLPNDetailId>569</OrderLPNDetailId>
                <sku>368</sku>
            </OrderLPNDetail>
        </OrderLPN>
        <OrderLPN><!--- 3rd Node -->
            <OrderLPNId>399</OrderLPNId>
            <OrderLPNDetail>
                <OrderLPNDetailId>845</OrderLPNDetailId>
                <sku>777</sku>
            </OrderLPNDetail>
            <OrderLPNDetail>
                <OrderLPNDetailId>846</OrderLPNDetailId>
                <sku>778</sku>
            </OrderLPNDetail>
        </OrderLPN>
    </OrderLines>
</Orders>

产生

<?xml version="1.0" encoding="utf-8"?>
<root>
    <seq id="345">0</seq>
    <seq id="346">1</seq>
    <seq id="399">2</seq>
</root>