Retrieving a value from XML and storing in a variable using XSLT

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<Result>
  <resultDetails>
    <resultDetailsData>
      <itemProperties>
        <ID>1</ID>
        <type>LEVEL</type> 
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">5</value> 
      </itemProperties>
    </resultDetailsData>
  </resultDetails>
</Result>

I have the xml described above. I want to get the value of the value tag (in this case "5") using the value of the type tag (that is, LEVEL in this case) and store it in a variable using XSLT so that I can use the variable later.

Any idea how to do this?

+5
source share
2 answers

You can do it as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">

<xsl:template match="/">
    <xsl:variable name="myVar" select="Result/resultDetails/resultDetailsData/itemProperties/value"/>
<varoutput>
    <xsl:value-of select="$myVar"/>
</varoutput>
</xsl:template> 

For your input XML, you get this output:

<?xml version="1.0" encoding="UTF-8"?>
<varoutput>5</varoutput>

Regards, Peter

+8
source

If you want to use the read variable to set the attribute (for example, the color of the string), you need to use { $ variable } , as shown below

<xsl:variable name="rColor" select="rowColor"/>

then

<fo:table-row background-color="{$rColor}">
+1

All Articles