【问题标题】:xslt-replace namespace uri alone单独的 xslt-replace 命名空间 uri
【发布时间】:2013-10-01 07:04:12
【问题描述】:

在我的 xml 中,命名空间 http://abc.com/source/error 应替换为 http://abc.com/error/i1。我必须使用xslt1.0,单独替换uri部分非常具有挑战性。所有其他都应该与输出一样。如果此命名空间不存在,则输入 xml 应按原样传递到输出。

我的输入 xml

<a xmlns:hj="http://abc.com/source/error">
<hj:b>sam</hj:b>
</a>

预期输出

<a xmlns:hj="http://abc.com/source/error/i1">
<hj:b>sam</hj:b>
</a>

【问题讨论】:

    标签: xml namespaces xslt-1.0


    【解决方案1】:

    这个 XSLT 将完成这项工作。但请注意,它仅在输入 XML 的命名空间前缀等于 hj: 时才有效

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hjold="http://abc.com/source/error" xmlns:hj="http://abc.com/source/error/i1" exclude-result-prefixes="hjold">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="a[namespace-uri-for-prefix('hj', /*) != '']">
            <a xmlns:hj="http://abc.com/source/error/i1">
                <xsl:apply-templates select="@*|node()" />
            </a>
        </xsl:template>
    
        <xsl:template match="hjold:*" >
            <xsl:element name="hj:{local-name()}">
                <xsl:apply-templates select="@*|node()" />
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="*" >
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="@*|node()" />
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    要在 XSLT 1.0 中使用并且即使没有声明命名空间,命名空间在输出中也无关紧要,请更改:

     <xsl:template match="a[namespace-uri-for-prefix('hj', /*) != '']">
    

     <xsl:template match="a">
    

    【讨论】:

    • 谢谢。对于 xslt,命名空间前缀无关紧要。即使是 xml,abc.com/source/error"> sam 也应该可以工作..xslt 版本也是 1.0
    • 对不起,但是如果你想在 XSLT 1.0 中使用它并且命名空间是否在结果中无关紧要,请将 &lt;xsl:template match="a[namespace-uri-for-prefix('hj', /*) != '']"&gt; 更改为 &lt;xsl:template match="a"&gt;
    • 标记,输入的xml可以是任何东西。也就是说,元素可以是任意数量。只有命名空间应该被替换。
    猜你喜欢
    • 2014-08-21
    • 2020-05-06
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多