XSLT - 如何按属性选择 XML 属性?

发布于 2024-07-13 09:21:34 字数 632 浏览 5 评论 0原文

这是我的源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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情何以堪。 2024-07-20 09:21:34

只需删除 Data 后面的斜杠并添加根:

<xsl:variable name="myVarA" select="/root/DataSet/Data[@Value1='2']/@Value2"/>

Just remove the slash after Data and prepend the root:

<xsl:variable name="myVarA" select="/root/DataSet/Data[@Value1='2']/@Value2"/>
乖乖 2024-07-20 09:21:34

您的 xpath 有两个问题 - 首先您需要像 phihag 提到的那样从 Data 之后删除子选择器。 此外,您还忘记在 xpath 中包含 root 。 这是您想要执行的操作:

select="/root/DataSet/Data[@Value1='2']/@Value2"

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 include root in your xpath. Here is what you want to do:

select="/root/DataSet/Data[@Value1='2']/@Value2"
燃情 2024-07-20 09:21:34

试试这个

xsl:variable name="myVarA" select="//DataSet/Data[@Value1='2']/@Value2" />

“//”将在任何深度搜索数据集

Try this

xsl:variable name="myVarA" select="//DataSet/Data[@Value1='2']/@Value2" />

The '//' will search for DataSet at any depth

帝王念 2024-07-20 09:21:34

注意:在 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.

怪我入戏太深 2024-07-20 09:21:34

我会通过创建一个变量来指向在 Value1 中具有正确值的节点,然后引用 t

<xsl:variable name="myVarANode" select="root//DataSet/Data[@Value1='2']" />
<xsl:value-of select="$myVarANode/@Value2"/>

其他人的答案也是正确的 - 事实上更正确,因为我没有注意到 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

<xsl:variable name="myVarANode" select="root//DataSet/Data[@Value1='2']" />
<xsl:value-of select="$myVarANode/@Value2"/>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文