xml - XSLT selecting one of two nodes with same name based on attribute value -


i have xml markup looks following:

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

in particular case, using xslt - what's optimal way create variable retrieve cat , dog ids? order never same /pet[1] wouldn't work. have 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"/> direct way, if want efficient access define key

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

and use

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

.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -