繁体   English   中英

XSLT根据属性值选择具有相同名称的两个节点之一

[英]XSLT selecting one of two nodes with same name based on attribute value

我有一些XML标记,如下所示:

<pet type="dog" id="76">
</pet>
<pet type="cat" id="79">
</pet>

在这种特殊情况下,使用XSLT - 创建变量来检索cat和dog id的最佳方法是什么? 订单永远不会相同,因此/pet[1]不起作用。 它必须是这样的东西:

<xsl:variable name="cat_id"><xsl:value-of select="...."/></xsl:variable>
<xsl:variable name="dog_id"><xsl:value-of select="...."/></xsl:variable>

那么<xsl:variable name="cat_id" select="//pet[@type = 'cat']/@id"/>是直接的方法,如果你想要有效的访问定义一个键

<xsl:key name="pet-by-type" match="pet" use="@type"/>

然后使用

<xsl:variable name="cat_id" select="key('pet-by-type', 'cat')/@id"/>

我会尝试这样的事情

<xsl:variable name="cat_id"><xsl:value-of select="pet[@type='cat']/@id "/ ></xsl:variable>

但它也取决于你在输入xml中的实际位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM