加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 命名空间停止XSLT工作

发布时间:2020-12-16 07:56:30 所属栏目:百科 来源:网络整理
导读:我有一个看起来像这样的XSLT: 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:cop
我有一个看起来像这样的XSLT:
<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,JAN</month>
            <season>Season11</season>
            <productId>22222</productId>
        </sets>
    </payload>
</Response>

由于< Response xmlns =“http://www.castiron.com/response”>中的名称空间.这导致整个XSLT失败.是否有可能取消命名空间,以便XSLT能够正常工作.如果删除命名空间并运行XSLT,它就能完美运行!

在XSLT中定义namesapce,即:
<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: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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读