如果指示的值在循环中不存在,则进行 XSL 测试,并显示结果一次



我是xsl的新手,广告我很难获得我想要的xml文件的结果。 这是我的XML文件的一部分,查询结果:

<?xml version="1.0" encoding="UTF-8"?>
-<SearchResults>
-<TableHeader>
<ColumnName>UCode</ColumnName>
<ColumnName>URev</ColumnName>
<ColumnName>Shapes</ColumnName>
<ColumnName>Name</ColumnName>
<ColumnName>Value</ColumnName>
</TableHeader>
-<Object>
<Attribute>XXXXXXX/Attribute>
<Attribute>A</Attribute>
<Attribute>BLABLA</Attribute>
<Attribute>PART_CODE</Attribute>
<Attribute>X123456</Attribute>
<Attribute/>
</Object>
-<Object>
<Attribute>YYYYYYYY/Attribute>
<Attribute>A</Attribute>
<Attribute>BLABLA</Attribute>
<Attribute>OPACITY</Attribute>
<Attribute>BLACK</Attribute>
<Attribute/>
</Object>
-<Object>
<Attribute>ZZZZZZZZ/Attribute>
<Attribute>A</Attribute>
<Attribute>BLABLA</Attribute>
<Attribute>PART_CODE</Attribute>
<Attribute>X198706</Attribute>
<Attribute/>
</Object>
-<Object>
<Attribute>XXXXXXX/Attribute>
<Attribute>A</Attribute>
<Attribute>BLABLA</Attribute>
<Attribute>OPACITY</Attribute>
<Attribute>BLACK</Attribute>
<Attribute/>
</Object>
</SearchResults>

这是我的 XSL 上的部分,当我有 Name='OPACITY' 和 Value='BLACK' 时,它只显示一次标签 EXIST~YES:

<xsl:for-each select="/queryResult/result/child::row">  
<xsl:variable name="attnamez" select='Name'/>
<xsl:if test="$attnamez='OPACITY'">
<xsl:variable name="attvaluez" select='Value'/>
<xsl:if test="$attvaluez!='NotDefined'">
<xsl:choose>
<xsl:when test="$attvaluez='BLACK'">
<xsl:choose>
<xsl:when test="count(preceding::row[Name='OPACITY' and Value=$attvaluez])=0">
&#x0A;EXIST~YES
</xsl:when>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:if>
</xsl:if>
</xsl:for-each>

我需要您的帮助来播放标签 EXIST~NO,以防 [名称='不透明度' 和 Value='BLACK'] 并非在所有行中都存在。 实际上,当我在for-each中使用"否则"块时,它会多次显示EXIST~NO(每次值不等于BLACK)。

非常感谢您的帮助,

下面是生成的 XML:

<queryResult>
<result>
<row idx="1">
<UCode>XXXXX</UCode> 
<URev>B</URev> 
<Name>OPACITY</Name> 
<Value>WHITE</Value> 
<SiteLegacy /> 
</row>
<row idx="2">
<UCode>YYYYYY</UCode> 
<URev>B</URev> 
<Name>OPACITY</Name> 
<Value>BLACK</Value> 
<SiteLegacy /> 
</row>
<row idx="3">
<UCode>YYYYYY</UCode> 
<URev>B</URev> 
<Name>OPACITY</Name> 
<Value>BLACK</Value> 
<SiteLegacy /> 
</row>
<row idx="4">
<UCode>YYYYYY</UCode> 
<URev>B</URev> 
<Name>OPACITY</Name> 
<Value>BLACK</Value> 
<SiteLegacy /> 
</row>
<row idx="5">
<UCode>YYYYYY</UCode> 
<URev>B</URev> 
<Name>OPACITY</Name> 
<Value>BLACK</Value> 
<SiteLegacy /> 
</row>
</result>
</queryResult>

按照你的XML,如果你想输出"EXIST~YES",如果至少有一个row名称='不透明度'和值='BLACK',或者"EXIST~NO"否则,你可以简单地这样做......

<xsl:choose>
<xsl:when test="queryResult/result/row[Name='OPACITY' and Value='BLACdK']">EXIST~YES</xsl:when>
<xsl:otherwise>EXIST~NO</xsl:otherwise>
</xsl:choose> 

根本不需要xsl:for-each

在 http://xsltfiddle.liberty-development.net/bnnZWk/1 看到它的实际效果

最新更新