JeremyC
JeremyC

Reputation: 1

Parsing XML Attributes Using XSLT

I need some XSLT to return some kind of output (anything really) based on one of the attributes in the source XML. This attribute consists of a number of name-value pairs so I need to be able to parse the whole text and pull out certain values and then do a comparison on them.

For example I may have some XML that looks like this:

<Element Where="Time=3,Successful=N"></Element>

and I want to return just something to say it took too long if the value of "Time" is greater than 10, or something to say it was unsuccessful if the value of "Successful" is "N".

The second aspect of that I have managed to achieve using the "contains" function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/Element">
        <xsl:if test="contains(@Where, 'Successful=N')">
            Message Failed
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

but I can't fathom how to parse out the "Time" value and then compare that to trigger an output if the value is >10.

Upvotes: 0

Views: 232

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

Try it this way?

<xsl:variable name="time" select="substring-before(substring-after(@Where, 'Time='), ',')" />
<xsl:if test="$time &gt; 10">
    <xsl:text>Too Long</xsl:text>
</xsl:if>

Note that this assumes there will always be a comma after the Time value. If not, add it yourself.

Upvotes: 1

Related Questions