我有以下 xml,其中包含产品信息。每个产品的描述有时会出现在下一个产品之前的下一行中,因为在一定数量的字符之后,描述会被拆分。
<?xml version="1.0" encoding="utf-8"?>
<root>
<row>
<rowWords>5.00</rowWords>
<rowWords>PRODUCTCODE1</rowWords>
<rowWords>DESCRIPTIONWORD1</rowWords>
<rowWords>DESCRIPTIONWORD2</rowWords>
<rowWords>DESCRIPTIONWORD3</rowWords>
<rowWords>DESCRIPTIONWORD4</rowWords>
<rowWords>DESCRIPTIONWORD5</rowWords>
<rowWords>11.28</rowWords>
<rowWords>56.40</rowWords>
</row>
<row>
<rowWords>DESCRIPTIONWORD6</rowWords>
</row>
<row>
<rowWords>6.00</rowWords>
<rowWords>PRODUCTCODE2</rowWords>
<rowWords>DESCRIPTIONWORD1</rowWords>
<rowWords>DESCRIPTIONWORD2</rowWords>
<rowWords>DESCRIPTIONWORD3</rowWords>
<rowWords>DESCRIPTIONWORD4</rowWords>
<rowWords>DESCRIPTIONWORD5</rowWords>
<rowWords>11.00</rowWords>
<rowWords>66.00</rowWords>
</row>
<row>
<rowWords>DESCRIPTIONWORD6</rowWords>
<rowWords>DESCRIPTIONWORD7</rowWords>
<rowWords>DESCRIPTIONWORD8</rowWords>
</row>
<row>
<rowWords>DESCRIPTIONWORD9</rowWords>
<rowWords>DESCRIPTIONWORD10</rowWords>
</row>
<row>
<rowWords>10.00</rowWords>
<rowWords>PRODUCTCODE3</rowWords>
<rowWords>DESCRIPTIONWORD1</rowWords>
<rowWords>DESCRIPTIONWORD2</rowWords>
<rowWords>10.00</rowWords>
<rowWords>100.00</rowWords>
</row>
.
. any amount of rows containing products
.
</root>
描述以及产品数量发生了变化,所以我不知道如何将产品描述放在一行中。
这是我已经完成的xsl。
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="something">
<xsl:output indent="yes"/>
<xsl:template match="/root">
<a:Invoice>
<Products>
<xsl:for-each select="row[position()>=5 and position()<=last()-5]">
<!-- With this if, i prevent create a product node with partial description-->
<xsl:if test="number(rowWords[1])">
<Product>
<xsl:attribute name="quantity"><xsl:value-of select="rowWords[1]"/></xsl:attribute>
<xsl:attribute name="productCode"><xsl:value-of select="rowWords[2]"/></xsl:attribute>
<!--Here is the problem (Thank you Dimitri) -->
<xsl:attribute name="description">
<xsl:call-template name="constructDescription">
<xsl:with-param name="pRow" select="."/>
<xsl:with-param name="pPosition" select="position()"/>
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="unitValue"><xsl:value-of select="rowWords[last()-1]"/></xsl:attribute>
<xsl:attribute name="amount"><xsl:value-of select="rowWords[last()]"/></xsl:attribute>
</Product>
</xsl:if>
</xsl:for-each>
</Products>
</a:Invoice>
</xsl:template>
<xsl:template name="constructDescription">
<xsl:param name="pRow"/>
<xsl:param name="pPosition"/>
<xsl:for-each select="$pRow/*[position() >= 3 and position() <= last() -2]">
<xsl:value-of select="concat(.,' ')"/>
</xsl:for-each>
<xsl:if test="$pRow/following-sibling::*[not(number(*[1])=number(*[1]))]">
<xsl:call-template name="constructDescription">
<xsl:with-param name="pRow" select="$pRow/following-sibling::
*[position()=number($pPosition)+1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
这是输出
<?xml version='1.0' ?>
<a:Invoice>
<Products>
<Product quantity="5.00" productCode="PRODUCTCODE1" description="DESCRIPTIONWORD1 DESCRIPTIONWORD2 DESCRIPTIONWORD3 DESCRIPTIONWORD4 DESCRIPTIONWORD5 DESCRIPTIONWORD6 DESCRIPTIONWORD7 DESCRIPTIONWORD8 DESCRIPTIONWORD9 DESCRIPTIONWORD1 DESCRIPTIONWORD2 DESCRIPTIONWORD3 DESCRIPTIONWORD4 DESCRIPTIONWORD5 DESCRIPTIONWORD6 DESCRIPTIONWORD7 DESCRIPTIONWORD8 DESCRIPTIONWORD9 " unitValue="11.28" amount="56.40"/>
<Product quantity="6.00" productCode="PRODUCTCODE2" description="DESCRIPTIONWORD1 DESCRIPTIONWORD2 DESCRIPTIONWORD3 DESCRIPTIONWORD4 DESCRIPTIONWORD5 DESCRIPTIONWORD6 DESCRIPTIONWORD7 DESCRIPTIONWORD8 DESCRIPTIONWORD9 " unitValue="11.00" amount="66.00"/>
<Product quantity="10.00" productCode="PRODUCTCODE3" description="DESCRIPTIONWORD1 DESCRIPTIONWORD2 " unitValue="10.00" amount="100.00"/>
</Products>
</a:Invoice>
发生了什么?问题是当我再次调用constructDescription!但我不知道如何解决它!
非常感谢!