命名空间停止XSLT工作 [英] Namespace Stopping XSLT Working

查看:74
本文介绍了命名空间停止XSLT工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的XSLT:

I have got an XSLT that looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:param name="month"/>
        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="month">
        <xsl:param name="month"/>
        <month>
            <xsl:choose>
                <xsl:when test="$month">
                    <xsl:value-of select="$month"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates/>
                </xsl:otherwise>
            </xsl:choose>
        </month>
    </xsl:template>

    <xsl:template name="splitMonths">
        <xsl:param name="months"/>
        <xsl:variable name="firstMonth" select="substring-before($months,',')"/>
        <xsl:variable name="month">
            <xsl:choose>
                <xsl:when test="$firstMonth">
                    <xsl:value-of select="$firstMonth"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$months"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="otherMonths" select="substring-after($months,',')"/>
        <xsl:if test="$month">
            <xsl:apply-templates>
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="$otherMonths">
            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="$otherMonths"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="payload">
        <payload>
            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="sets/month"/>
            </xsl:call-template>
        </payload>
    </xsl:template>

</xsl:stylesheet>

这是输入:

<?xml version="1.0" encoding="UTF8"?>
 <Response xmlns="http://www.castiron.com/response">
    <code>0</code>
    <message>Success</message>
     <payload>
         <sets>
            <month>AUG,SEP,OCT,NOV,DEC,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,JAN</month>
            <season>Season11</season>
            <productId>11111</productId>
        </sets>
    </payload>
</Response>

由于<Response xmlns="http://www.castiron.com/response">标记中的名称空间,这导致整个XSLT也失败.是否有可能取消命名空间的限制,以便XSLT能够正常工作.如果删除命名空间并运行XSLT,则效果很好!

Because of the name space in the <Response xmlns="http://www.castiron.com/response"> tags this is causing the whole XSLT too fail. Would it be possible to illiminate the namespace so that the XSLT will work. If you remove the namespace and run the XSLT it works perfectly!

推荐答案

在XSLT中定义名称空间,即:

Define namesapce in XSLT, i.e.:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:r="http://www.castiron.com/response" exclude-result-prefixes="r">

    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:param name="month"/>
        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="r:month">
        <xsl:param name="month"/>
        <month xmlns="http://www.castiron.com/response">
            <xsl:choose>
                <xsl:when test="$month">
                    <xsl:value-of select="$month"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates/>
                </xsl:otherwise>
            </xsl:choose>
        </month>
    </xsl:template>

    <xsl:template name="splitMonths">
        <xsl:param name="months"/>
        <xsl:variable name="firstMonth" select="substring-before($months,',')"/>
        <xsl:variable name="month">
            <xsl:choose>
                <xsl:when test="$firstMonth">
                    <xsl:value-of select="$firstMonth"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$months"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="otherMonths" select="substring-after($months,',')"/>
        <xsl:if test="$month">
            <xsl:apply-templates>
                <xsl:with-param name="month" select="$month"/>
            </xsl:apply-templates>
        </xsl:if>
        <xsl:if test="$otherMonths">
            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="$otherMonths"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template match="r:payload">
        <payload xmlns="http://www.castiron.com/response">
            <xsl:call-template name="splitMonths">
                <xsl:with-param name="months" select="r:sets/r:month"/>
            </xsl:call-template>
        </payload>
    </xsl:template>

</xsl:stylesheet>

这篇关于命名空间停止XSLT工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆