我正在尝试根据位置将element1标签与(键和价格(分组,我可以以10的倍数接收元素1,例如30或40。 但是我在将它们分组时遇到问题,例如前 10 个将是元素 1,第二组应命名为 元素 2,依此类推
我无法正确对它们进行分组,也无法重命名组。
XSLT:
<xsl:for-each select="element1">
<xsl:element name="element{position()}">
<xsl:element name="key"><xsl:value-of select="key"/></xsl:element>
<xsl:element name="price"><xsl:value-of select="price"/></xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
输入:
<?xml version="1.0" encoding="UTF-8"?>
<pricingCondition>
<element1>
<key>TAX</key>
<price>0</price>
</element1>
<element1>
<key>TAX1</key>
<price>2</price>
</element1>
<element1>
<key>RATE</key>
<price>7</price>
</element1>
<element1>
<key>PRICE</key>
<price>24</price>
</element1>
<element1>
<key>FREIGHTVALUE</key>
<price>0</price>
</element1>
<element1>
<key>NET</key>
<price>25</price>
</element1>
<element1>
<key>TAX2</key>
<price>1</price>
</element1>
<element1>
<key>TAX3</key>
<price>30</price>
</element1>
<element1>
<key>GROSSP</key>
<price>267</price>
</element1>
<element1>
<key>GROSSV</key>
<price>4300</price>
</element1>
<element1>
<key>TAX</key>
<price>01</price>
</element1>
<element1>
<key>TAX1</key>
<price>02</price>
</element1>
<element1>
<key>RATE</key>
<price>73</price>
</element1>
<element1>
<key>PRICE</key>
<price>247</price>
</element1>
<element1>
<key>FREIGHTVALUE</key>
<price>0</price>
</element1>
<element1>
<key>NET</key>
<price>15</price>
</element1>
<element1>
<key>TAX2</key>
<price>11</price>
</element1>
<element1>
<key>TAX3</key>
<price>10</price>
</element1>
<element1>
<key>GROSSP</key>
<price>167</price>
</element1>
<element1>
<key>GROSSV</key>
<price>2300</price>
</element1>
</pricingCondition>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<pricingCondition>
<element1>
<key>TAX</key>
<price>0</price>
</element1>
<element1>
<key>TAX1</key>
<price>2</price>
</element1>
<element1>
<key>RATE</key>
<price>7</price>
</element1>
<element1>
<key>PRICE</key>
<price>24</price>
</element1>
<element1>
<key>FREIGHTVALUE</key>
<price>0</price>
</element1>
<element1>
<key>NET</key>
<price>25</price>
</element1>
<element1>
<key>TAX2</key>
<price>1</price>
</element1>
<element1>
<key>TAX3</key>
<price>30</price>
</element1>
<element1>
<key>GROSSP</key>
<price>267</price>
</element1>
<element1>
<key>GROSSV</key>
<price>4300</price>
</element1>
<element2>
<key>TAX</key>
<price>01</price>
</element2>
<element2>
<key>TAX1</key>
<price>02</price>
</element2>
<element2>
<key>RATE</key>
<price>73</price>
</element2>
<element2>
<key>PRICE</key>
<price>247</price>
</element2>
<element2>
<key>FREIGHTVALUE</key>
<price>0</price>
</element2>
<element2>
<key>NET</key>
<price>15</price>
</element2>
<element2>
<key>TAX2</key>
<price>11</price>
</element2>
<element2>
<key>TAX3</key>
<price>10</price>
</element2>
<element2>
<key>GROSSP</key>
<price>167</price>
</element2>
<element2>
<key>GROSSV</key>
<price>2300</price>
</element2>
</pricingCondition>
AFAICT,你想做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/pricingCondition">
<xsl:copy>
<xsl:for-each select="element1">
<xsl:element name="element{ceiling(position() div 10)}">
<xsl:copy-of select="*"/>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>