在XSLT中分配XML属性值时出错

时间:2015-07-05 19:07:20

标签: xslt

我的输入XML如下:

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

我的要求是编写XSLT并检查<Error>标记是否具有“服务器无法访问”的值,如果是,则将Status属性值更改为“3”。

我在下面写了代码,但收到了错误:

  

“XSLT错误:对于属性”{1}“,预期打开引号   元素类型为“状态”。“

请协助。

<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 个答案:

答案 0 :(得分:1)

试试这个:

<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>

答案 1 :(得分:0)

或者很快:

qmethod
相关问题