【发布时间】:2011-03-24 19:32:17
【问题描述】:
我正在尝试计算 XML/Attributes/Attribute[@Type='ComplexAttr'] 的位置。如果存在,则执行此操作,否则执行其他操作。但是,计数始终为零。有人可以告诉我我错过了什么。 也有人可以指导我如何改进我使用很多的 xslt 的最后一部分 xsl:if 语句。 提前致谢。
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="type" match="Attribute" use="Type"/>
<xsl:template match="/">
<Data Schema="XML A">
<xsl:apply-templates select="XML/Attributes/Attribute[generate-id() = generate-id(key('type', Type)[1])]">
<xsl:sort select="Type" order="descending"/>
</xsl:apply-templates>
<errorCodes>
<xsl:apply-templates select="XML/Attributes/Attribute" mode="errors"/>
</errorCodes>
</Data>
</xsl:template>
<xsl:template match="Attribute">
<xsl:variable name="compType">
<xsl:value-of select="count(XML/Attributes/Attribute[Type='ComplexAttr'])"/>
</xsl:variable> />
<!--<xsl:value-of select="count($compType)"/>-->
<xsl:if test="Type!='ComplexAttr'">
<Attributes type="{Type}">
<xsl:apply-templates select="key('type',Type)" mode="out"/>
<xsl:if test="Type='common'">
<Collection id="" name="test">
<ComplexAttr refId="0">
<MaskValue />
<xsl:choose>
<xsl:when test="$compType > 0">
<xsl:apply-templates select="key('type','ComplexAttr')" mode="out"/>
</xsl:when>
<xsl:otherwise>
<Attr id="" name="Color" value="000"/>
<Attr id="" name="Size" value="0010"/>
<Attr id="" name="UPC" value=""/>
<Attr id="" name="Style#" value=""/>
<Attr id="" name="Exclude" value=""/>
</xsl:otherwise>
</xsl:choose>
</ComplexAttr>
</Collection>
</xsl:if>
</Attributes>
</xsl:if>
</xsl:template>
<xsl:template match="Attribute" mode="out">
<Attr id="{id}" name="{Name}" value="{Value}"/>
</xsl:template>
<xsl:template match="Attribute[Type='ComplexAttr']" mode="out">
<Attr id="{id}" name="{Name}" value="{Value}"/>
</xsl:template>
<xsl:template match="Attribute" mode="errors">
<xsl:if test="Name['Buyer ID' or 'Coordinator ID' or 'Retail' or
'Master Pack Qty' or 'Master Pack Height' or 'Master Pack Length' or 'Master Pack Weight' or
'Master Pack Width' or 'Product Description' or 'PO Cost' or 'GTIN' or 'Vendor Model'] and Value=''">
<errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
示例 XML:
<?xml version="1.0" encoding="windows-1252"?>
<XML>
<Attributes>
<Attribute>
<id>5</id>
<Name>Buyer ID</Name>
<Type>common</Type>
<Value>Lee</Value>
</Attribute>
<Attribute>
<id>331</id>
<Name>Enviornment</Name>
<Type>common</Type>
<Value>Development</Value>
</Attribute>
<Attribute>
<id>79</id>
<Name>Retail</Name>
<Type>common</Type>
<Value></Value>
</Attribute>
<Attribute>
<id>402</id>
<Name>Gender</Name>
<Type>category</Type>
<Value>Men</Value>
</Attribute>
<Attribute>
<id>1197</id>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>Testing</Value>
<Path />
</Attribute>
</Attributes>
输出: 如您所见,它没有给我预期的输出。它应该是 Attributes/Attribute[Type='common']/Collection/ComplexAttr/ 我得到以下
<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
<Attributes type="common">
<Attr id="5" name="Buyer ID" value="Lee" />
<Attr id="331" name="Enviornment" value="Development" />
<Attr id="79" name="Retail" value="" />
<Attr id="41" name="PlusShip" value="False" />
<Collection id="" name="test">
<ComplexAttr refId="0">
<MaskValue />
<Attr id="" name="Color" value="000" />
<Attr id="" name="Size" value="0010" />
<Attr id="" name="UPC" value="" />
<Attr id="" name="Style#" value="" />
<Attr id="" name="Exclude" value="" />
</ComplexAttr>
</Collection>
</Attributes>
<Attributes type="category">
<Attr id="402" name="Gender" value="Men" />
<Attr id="433" name="HeelHeight" value="" />
</Attributes>
<errorCodes>
<errorCode>"value for Retail is missing."</errorCode>
</errorCodes>
</Data>
【问题讨论】:
-
您说“我正在尝试计算 XML/Attributes/Attribute[@Type='ComplexAttr'] 的位置。”我在您的示例中的任何地方都找不到该代码。
-
@Micheal Kay。根据与@Iwburk 的讨论,我正在更改我的代码。我已经上传了更新的 XSLT。如果你看到了什么,请告诉我。谢谢
-
@Michael - 对,但他确实有
/XML/Attributes/Attribute[Type='ComplexAttr'],这就是我认为的意思 -
我已经发布了一个更新的答案。希望这对您更有效。
-
好问题,+1。有关问题的解释和简单的解决方案,请参阅我的答案。 :)
标签: xslt attributes count