2

My input XML is as below:

<MessageOutput Status="2">
    <Source>External</Source>
    <Error>Server not reachable</Error>
    <LineNo>0</LineNo>
</MessageOutput>

My requirement is to write XSLT and check if <Error> tag has value "Server not reachable" and if yes then change the Status attribute value to "3".

I wrote below code but getting error:

"XSLT Error: Open quote is expected for attribute "{1}" associated with an element type "Status"."

Please assist.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="ISO-8859-1"/>

    <xsl:variable name="Des" select="'Server not reachable'"/>
    <xsl:variable name="Err" select="MessageOutput/Error"/>

    <xsl:template match="*">
        <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/MessageOutput">
        <xsl:choose>
            <xsl:when test="$Des=$Err">
                <MessageOutput Status="3">
                    <xsl:apply-templates/>
                </MessageOutput>
             </xsl:when>
             <xsl:otherwise>
                <MessageOutput Status=<xsl:value-of select="@Status"/>>
                    <xsl:apply-templates/>
                </MessageOutput>
             </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

2 Answers 2

2

Try this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>

    <xsl:variable name="Des" select="'Server not reachable'"/>
    <xsl:variable name="Err" select="MessageOutput/Error"/>

    <xsl:template match="*">
        <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/MessageOutput">
        <xsl:choose>
            <xsl:when test="$Des=$Err">
                <MessageOutput Status="3">
                    <xsl:apply-templates/>
                </MessageOutput>
             </xsl:when>
             <xsl:otherwise>
                <MessageResult Status="{@Status}">
                    <xsl:apply-templates/>
                </MessageResult>
             </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

1 Comment

Done. Once again Thanks a lot.
1

Or shortly:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="MessageOutput[Error='Server not reachable']/@Status">
    <xsl:attribute name="Status">3</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.