首页 > 解决方案 > 使用 XSL 根据 XML 的条件添加命名空间

问题描述

我正在尝试根据条件将名称空间添加到 xml。但条件不起作用。有人可以帮忙吗。

输入 XML:

                    <n0:MainTag xmlns:n0='http://abc123.com' xmlns:prx='urn:testing.com' xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/'>
                    <header>M</header>
                    <Data>
                        <Child>623471568753</Child>

                    </Data>
                </n0:MainTag>

输出 XML:

                    <?xml version="1.0" encoding="UTF-8"?>
                <ns0:MainTag xmlns:ns0="http://xyz987.com">
                    <header>M</header>
                    <Data>
                        <Child>623471568753</Child>
                    </Data>
                </n0:MainTag>

第二个输入:输入 XML:

                <n0:DifferentTag xmlns:n0='http://abc123.com' xmlns:prx='urn:testing.com' xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/'>
                <header>M</header>
                <Datum>
                    <Child>Test123</Child>

                </Datum>
            </n0:DifferentTag>

输出 XML:

                <n0:DifferentTag xmlns:ns0="http://QWR.com">
                <header>M</header>
                <Datum>
                    <Child>Test123</Child>

                </Datum>
            </n0:DifferentTag>

XSL 试过:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                xmlns:ns0="http://xyz987.com">
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>

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

<xsl:template match="MainTag">
    <xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="MainTag">
    <xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
        <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>

条件:检查源 XML 中的标签名称

标签: xmlxsltnamespaces

解决方案


问题在于,在您的输入 XML 中,两者MainTagDifferentTag在“ http://abc123.com ”中,但是您在 XSLT 中考虑到了这一点,因此它试图匹配没有命名空间中的标签。

您需要在 XSLT 中声明前缀,并在匹配中使用它。

另请注意,您当前的 XSLT 有两个模板匹配MainTag,而其中一个可能应该匹配DifferentTag

试试这个 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:n0="http://abc123.com"
                              exclude-result-prefixes="n0">
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>

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

<xsl:template match="n0:MainTag">
    <xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="n0:DifferentTag">
    <xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
        <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>

编辑:如果您真的不知道输入 XML 中的命名空间,请尝试使用此 XSLT...

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

<xsl:output encoding='UTF-8' indent='yes' method='xml'/>

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

<xsl:template match="*[local-name() = 'MainTag']">
    <xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="*[local-name() = 'DifferentTag']">
    <xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
        <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>

推荐阅读