I have some XML Markup that looks like the following:
<pet type="dog" id="76">
</pet>
<pet type="cat" id="79">
</pet>
In this particular case, using XSLT - what's the most optimal way to create a variable to retrieve the cat and dog ids? The order is never the same so /pet[1]
wouldn't work. It would have to be something such as:
<xsl:variable name="cat_id"><xsl:value-of select="...."/></xsl:variable>
<xsl:variable name="dog_id"><xsl:value-of select="...."/></xsl:variable>
Well <xsl:variable name="cat_id" select="//pet[@type = 'cat']/@id"/>
is the direct way, if you want efficient access define a key
<xsl:key name="pet-by-type" match="pet" use="@type"/>
and then use
<xsl:variable name="cat_id" select="key('pet-by-type', 'cat')/@id"/>
.
I would try something like this
<xsl:variable name="cat_id"><xsl:value-of select="pet[@type='cat']/@id "/ ></xsl:variable>
But it also depends on your actual position in input xml.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.