First of all, I am completly new to xslt. I have a program that gives me an xml file with the following layout
<?xml version="1.0" encoding="utf-8"?>
<Statistic>
<Elements>
<Element ElementName="A" Type="Element">
<ElementResult StatType="Mean"></ElementResult>
<ElementResult StatType="Reported">
<ResultValue>10</ResultValue>
</ElementResult>
</Element>
</Elements>
<Elements>
<Element ElementName="B" Type="Element">
<ElementResult StatType="Mean"></ElementResult>
<ElementResult StatType="Reported">
<ResultValue>5</ResultValue>
</ElementResult>
</Element>
</Elements>
<Elements>
<Element ElementName="C" Type="Element">
<ElementResult StatType="Mean"></ElementResult>
<ElementResult StatType="Reported">
<ResultValue>0.5</ResultValue>
</ElementResult>
</Element>
</Elements>
<Elements>
<Element ElementName="D" Type="Element">
<ElementResult StatType="Mean"></ElementResult>
<ElementResult StatType="Reported">
<ResultValue>0.0001</ResultValue>
</ElementResult>
</Element>
</Elements>
<Elements>
<Element ElementName="E" Type="Element">
<ElementResult StatType="Mean"></ElementResult>
<ElementResult StatType="Reported">
<ResultValue>0.1</ResultValue>
</ElementResult>
</Element>
</Elements>
</Statistic>
I want to extract only the values in a other order. The programm allows me to use a xslt stylesheet to transform my output. My idea was, to use nodelist in the stylesheet, because there a much more elements I would like to extract. Here is my code what I have:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
version="1.0">
<xsl:output method="text" />
<xsl:variable name="ElementArray">
<ElementItem Item="C" />
<ElementItem Item="D" />
<ElementItem Item="A" />
<ElementItem Item="B" />
<ElementItem Item="E" />
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="msxsl:node-set($ElementArray)/ElementItem">
<xsl:value-of select="@Item"/> <!-- This line works as excpected --> CDABE
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My problem is now, how can I use the @Item in the following line.
<xsl:value-of select="/Statistic/Elements/Element[@ElementName=***@Item must be included here, but how***]/ElementResult[@StatType='Reported']/ResultValue" />
<!-- This line should give in the end --> 0.5 0.0001 10 5 0.1
I hope anyone has an idea how I can make this.
Edit: corrected output and xml file.
I am inclined towards an entirely different method to approach this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="element-names">C D A B E </xsl:param>
<xsl:key name="elem" match="Element" use="@ElementName" />
<xsl:template match="/">
<xsl:value-of select="$element-names"/>
<xsl:text> </xsl:text>
<xsl:call-template name="process"/>
</xsl:template>
<xsl:template name="process">
<xsl:param name="element-names" select="$element-names" />
<xsl:if test="$element-names">
<xsl:variable name="current-name" select="substring-before($element-names, ' ')" />
<xsl:value-of select="key('elem', $current-name)/ElementResult[@StatType='Reported']/ResultValue"/>
<xsl:text> </xsl:text>
<xsl:call-template name="process">
<xsl:with-param name="element-names" select="substring-after($element-names, ' ')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Using your input example, this returns:
C D A B E
0.5 0.0001 10 5 0.1