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

xml – 使用xslt更改元素的命名空间

发布时间:2020-12-15 23:53:50 所属栏目:百科 来源:网络整理
导读:我想做到这一点 hello one paragraphp1/paragraph /one/hello 进入这个 x:hello y:an_attribute="a value for an_attribute" xmlns:x="some_new_namespace" xmlns:y="other_ns" one paragraphp1/paragraph /one/x:hello 这是我提出的样式表: xsl:stylesheet
我想做到这一点
<hello>
  <one>
    <paragraph>p1</paragraph>
  </one>
</hello>

进入这个

<x:hello y:an_attribute="a value for an_attribute" xmlns:x="some_new_namespace" xmlns:y="other_ns">
  <one>
    <paragraph>p1</paragraph>
  </one>
</x:hello>

这是我提出的样式表:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:param name="element_localname" select="'hello'"/>


    <xsl:template match="node()">
        <xsl:choose>
            <xsl:when test="local-name() = $element_localname">
                <xsl:element name="{$element_localname}" namespace="some_new_namespace">
                    <xsl:attribute name="an_attribute" namespace="other_ns">a value for an_attribute</xsl:attribute>
                    <xsl:apply-templates select="node()"/>
                </xsl:element>
            </xsl:when>

            <!-- copy the rest as is -->
            <xsl:otherwise> 
                <xsl:copy>
                    <xsl:apply-templates select="node()" />
                </xsl:copy>
            </xsl:otherwise>

        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

但由于一些奇怪的原因,我添加到元素的属性与根元素本身具有相同的命名空间?为什么?

<ns0:hello xmlns:ns0="other_ns" ns0:an_attribute="a value for an_attribute">
  <one>
    <paragraph>p1</paragraph>
  </one>
</ns0:hello>

谢谢你的阅读.

这个样式表:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="some_new_namespace"
    xmlns:y="other_ns">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="element_localname" select="'hello'"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="local-name() = $element_localname">
                <xsl:element name="x:{$element_localname}">
                    <xsl:attribute name="y:an_attribute">
                        <xsl:text>a value for an_attribute</xsl:text>
                    </xsl:attribute>
                    <xsl:apply-templates select="node()|@*"/>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="identity" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

输出:

<x:hello y:an_attribute="a value for an_attribute"
         xmlns:y="other_ns" xmlns:x="some_new_namespace">
    <one>
        <paragraph>p1</paragraph>
    </one>
</x:hello>

(编辑:李大同)

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

    推荐文章
      热点阅读