简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM