我有一个这样的 xml 文件:
<products>
<cars>
<car>
<brand>Audi</brand>
<type>S3</type>
<attribs>
<attrib>
<color>1</color>
<price>45000</price>
</attrib>
<attrib>
<color>2</color>
<price>75000</price>
</attrib>
<attrib>
<color>4</color>
<price>35000</price>
</attrib>
</attribs>
</car>
<!-- Many cars following -->
</cars>
<colors>
<color>
<id>1</id>
<shortdesc>Blue</shortdesc>
<description>Blue Lagoon</description>
</color>
<color>
<id>2</id>
<shortdesc>Red</shortdesc>
<description>Red Sport</description>
</color>
<color>
<id>3</id>
<shortdesc>Green</shortdesc>
<description>Green Forest</description>
</color>
<color>
<id>4</id>
<shortdesc>Yellow</shortdesc>
<description>Yellow</description>
</color>
<!-- many colors -->
</colors>
</products>
我正在将这个 XML 转换为一个 excel 交叉表,其中汽车在垂直方向,颜色在水平方向,如下所示:
Brand / Model 1/Blue 2/Red 3/Green 4/Yellow
Audi S3 45000 75000 - 35000
Audi S6 66000 68000 59000 -
Jaguar x-type 98000 - 99500 -
xslt 看起来像:
<ss:Table>
<ss:Row>
<!-- This is the header row -->
<ss:Cell>
<ss:Data ss:Type="String">Brand / Model</ss:Data>
</ss:Cell>
<xsl:for-each select="colors/color">
<ss:Cell>
<ss:Data ss:Type="String">
<xsl:value-of select="id"/>/<xsl:value-of select="shortdesc"/>
</ss:Data>
</ss:Cell>
</xsl:for-each>
</ss:Row>
<xsl:for-each select="cars/car">
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">
<xsl:value-of select="brand"/> <xsl:value-of select="type"/>
</ss:Data>
</ss:Cell>
<xsl:for-each select="attribs/attrib">
<!-- I Know this is incorrect, but what to put in the if ? -->
<xsl:if test="color = /colors/color/id">
<ss:Cell>
<ss:Data ss:Type="String">
<xsl:value-of select="price"/>
</ss:Data>
</ss:Cell>
</xsl:if>
<xsl:if test="color != /colors/color/id">
<ss:Cell>
<ss:Data ss:Type="String">-</ss:Data>
</ss:Cell>
</xsl:if>
</xsl:for-each>
</ss:Row>
</xsl:for-each>
</ss:Table>
我正在寻找在右栏中比较/写价格的方法。
该模板引用节点 /products 以便能够访问汽车和颜色。一种可能的方法是在我在标题中写入颜色的同时创建一个数组,并在处理汽车时与它进行比较;这是一种可能的方法还是可能有更好的方法?
另一个细节:我无法更改 XML,因为它已经像这样设计了(实际上我正在处理的不是汽车,只是为了让它更简单)