xpath查找当前节点的值
我有几个具有相同 Name='UPC' 的节点,我需要找到当前节点的值。
<XML>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>Testing</Value>
</Attribute>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>24a</Value>
</Attribute>
</XML>
预期输出: 它应该从 /Attribute/Value 中提取值,其中 Name='UPC' 且 Type = 'ComplexAttr'。
第一次运行=“测试”& 在第二次运行时,该值应为 = '24a'
我尝试使用以下代码,但它不起作用。该值为空。
<xsl:attribute name ="value">
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][$i]/Value" />
</xsl:attribute>
其中 $i 是我用来循环上述 xml 的变量,每次运行后它都会递增。但是,它在每次运行中只给我相同的值“测试”(这是第一个值)。我已经检查了变量的值。每次循环时它都会发生变化。
我也尝试过使用 current() 和position() 如下所示,但在这种情况下我得到了 null 。
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][current()]/Value" />
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][position() = $i]/Value" />
有人可以帮我解决这个问题吗?提前致谢。
I have several nodes with same Name='UPC' and I need to find the value of the current one.
<XML>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>Testing</Value>
</Attribute>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>24a</Value>
</Attribute>
</XML>
Expected Output:
It should pull the value from /Attribute/Value where Name='UPC' and Type = 'ComplexAttr'.
On the first run = 'Testing' &
On the 2nd run the value should be = '24a'
I'm trying to use the following code but it is not working. The value is null.
<xsl:attribute name ="value">
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][$i]/Value" />
</xsl:attribute>
where $i is the variable I'm using to loop through the above xml and it increments after each run. However, it only gives me the same value 'Testing' (which is the first value) in every run. I have checked the value of the variable. It is changing every time it loops through.
I have also tried using current() and position() like below but I'm getting null in this case.
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][current()]/Value" />
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][position() = $i]/Value" />
Can someone help me out with this. Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是最大的常见问题解答之一:
[]
运算符的绑定比//
缩写更强。为了选择 XML 文档中满足谓词中特定条件的第一个元素,请使用:
为了选择 XML 文档中满足特定条件的第二个元素在谓词中,使用:
为了选择 XML 文档中满足谓词中特定条件的第
$i
个元素,请使用:This is one of the biggest FAQ:
The
[]
operator binds stronger than the//
abbreviation.In order to select the 1st element in the XML document, that satisfies the specific condition in the predicate, use:
In order to select the 2nd element in the XML document, that satisfies the specific condition in the predicate, use:
In order to select the
$i
th element in the XML document, that satisfies the specific condition in the predicate, use:您不能在 XPath 表达式中使用变量。尝试手动使用常量,您会发现它有效:
一般来说,您实际上并没有在 XSLT 中编写循环,即使语法允许这样做。您编写在特定时间点通过特定上下文调用的模板。如果不了解整个计划的背景,我不确定下一步最好的步骤是什么。
You can't use a variable in an XPath expression. Try manually using a constant and you'll see that it works:
In general, you don't really write loops in XSLT, even though the syntax allows it. You write templates that are invoked with a particular context at a particular point in time. I'm not sure what the best next step is without knowing more about the context of the overall program.