...">

How to save an XML attribute (with an XML value passing some test) in an XSL variable?

Having the following xml:

<countries>
  <country id="1">Andora</country>
  <country id="2">Bulgaria</country>
  <country id="3">Croatia</country>
  <country id="4">Danemark</country>
  <country id="5">Estonia</country>
</countries>

How can I save the Andora attribute "@id" in the xsl variable :? Sort of

<xsl:variable name="andora_id" select=???????>

so that I can use "$ andora_id" with a value of "1" later in the rest of my xsl?

+3
source share
2 answers

How can I save the @id attribute? Andora in xsl variable: variable? Something like

<xsl:variable name="andora_id" select=???????>

so that I can use "$ andora_id" with a value of "1" later in the rest of my xsl?

This is an XPath question.

Using

<xsl:variable name="andora_id" select="/*/country[.='Andora']/@id"/>

xsl:variable andora_id, node -set id country, XML 'Andora'.

+3
<xsl:variable name="andora_id" 
              select="/countries/country[text() = 'Andora']/@id" />
+2

Source: https://habr.com/ru/post/1785784/


All Articles