我想写一个 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>
提前致谢。