XSLT - 如何按属性选择 XML 属性?
这是我的源xml的结构:
<root>
<DataSet Value="A">
<Data Value1="1" Value2="anythingA1" />
<Data Value1="2" Value2="anythingA2" />
<Data Value1="3" Value2="anythingA3" />
<Data Value1="4" Value2="anythingA4" />
<Data Value1="5" Value2="anythingA5" />
</DataSet>
</root>
我喜欢从中创建一些变量,例如从所有Value1 =“2”和所有Value1 =“5”应该导致myVar1与anythingA2和myVar2与anythingA5
我的方法看起来像这样
<xsl:variable name="myVarA" select="/DataSet/Data/[@Value1='2']/@Value2" />
,但是由于 Value2 不是 Value1 的子级,因此课程无法正常工作。
感谢您提前提供任何提示!
this is the structure of my source xml:
<root>
<DataSet Value="A">
<Data Value1="1" Value2="anythingA1" />
<Data Value1="2" Value2="anythingA2" />
<Data Value1="3" Value2="anythingA3" />
<Data Value1="4" Value2="anythingA4" />
<Data Value1="5" Value2="anythingA5" />
</DataSet>
</root>
from which I like to create some variables e.g. from all with Value1="2" and all with Value1="5" should result myVar1 with anythingA2 and myVar2 with anythingA5
My approch looks like this
<xsl:variable name="myVarA" select="/DataSet/Data/[@Value1='2']/@Value2" />
but of course is not working since Value2 is no child of Value1.
thanks for any hints in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需删除
Data
后面的斜杠并添加根:Just remove the slash after
Data
and prepend the root:您的 xpath 有两个问题 - 首先您需要像 phihag 提到的那样从
Data
之后删除子选择器。 此外,您还忘记在 xpath 中包含root
。 这是您想要执行的操作:There are two problems with your xpath - first you need to remove the child selector from after
Data
like phihag mentioned. Also you forgot to includeroot
in your xpath. Here is what you want to do:试试这个
“//”将在任何深度搜索数据集
Try this
The '//' will search for DataSet at any depth
注意:在 xpath 的开头使用 // 会占用一点 CPU 资源——它将搜索每个节点以查找匹配项。 使用更具体的路径(例如 /root/DataSet)将创建更快的查询。
Note: using // at the beginning of the xpath is a bit CPU intensitve -- it will search every node for a match. Using a more specific path, such as /root/DataSet will create a faster query.
我会通过创建一个变量来指向在 Value1 中具有正确值的节点,然后引用 t
其他人的答案也是正确的 - 事实上更正确,因为我没有注意到 XPATH 中的额外斜杠会弄乱事情发生了。 尽管如此,这仍然有效,并且可能适用于不同的事情,因此请将此方法保留在您的工具箱中。
I would do it by creating a variable that points to the nodes that have the proper value in Value1 then referring to t
Everyone else's answers are right too - more right in fact since I didn't notice the extra slash in your XPATH that would mess things up. Still, this will also work , and might work for different things, so keep this method in your toolbox.