使用XSLT1.0计数当前节点中的前身节点

  • 本文关键字:节点 XSLT1 使用 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-e EAP),当我在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>等...

请建议。

此样式表

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

相关内容

  • 没有找到相关文章

最新更新