我想寫一個xsl轉換,但是卡在「counter」部分。 這基本上就是我想做的事:XSL計數器循環轉換
輸入文件:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Pallets>
<Pallet>
<Line>
<Product>test</Product>
</Line>
<Line>
<Product>test2</Product>
</Line>
</Pallet>
<Pallet>
<Line>
<Product>test_1</Product>
</Line>
<Line>
<Product>test_2</Product>
</Line>
</Pallet>
</Pallets>
</root>
而這正是我想要的輸出是:
<?xml version="1.0" encoding="utf-8"?>
<Result>
<Pallet>
<ID>2</ID> ==> This is a counter that increments starting from 2
<ID2>1</ID2> ==> Always "1"
<Line>
<ID>3</ID> ==> The counter from above that increments
<ParentID>2</ParentID> ==> PalletID (ID from above the loop)
<Name>test</Name>
</Line>
<Line>
<ID>4</ID> ==> The counter from above that increments
<ParentID>2</ParentID> ==> PalletID
<Name>test2</Name>
</Line>
</Pallet>
<Pallet>
<ID>5</ID> ==> The counter from above that increments
<ID2>1</ID2> ==> Always "1"
<Line>
<ID>6</ID> ==> The counter from above that increments
<ParentID>5</ParentID> ==> PalletID
<Name>test_1</Name>
</Line>
<Line>
<ID>7</ID> ==> The counter from above that increments
<ParentID>5</ParentID> ==> PalletID
<Name>test_2</Name>
</Line>
</Pallet>
</Result>
誰能幫助我? 這就是我迄今爲止的內容,但正如您將看到的,palletId的計數器不正確。第二個PalletID應該有ID = 5而不是3:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Result>
<xsl:for-each select="root/Pallets/Pallet">
<xsl:variable name="counter" select="1" />
<Pallet>
<xsl:variable name="Parentcounter" select="position() + $counter" />
<ID>
<xsl:value-of select="$Parentcounter"/>
</ID>
<ID2>1</ID2>
<xsl:for-each select="Line">
<Line>
<ID>
<xsl:value-of select="$Parentcounter + position()"/>
</ID>
<ParentID>
<xsl:value-of select="$Parentcounter"/>
</ParentID>
<Name>
<xsl:value-of select="Product"/>
</Name>
</Line>
</xsl:for-each>
</Pallet>
</xsl:for-each>
</Result>
</xsl:template>
</xsl:stylesheet>
在此先感謝。
謝謝。我用模板的解決方案:) –