使用XSLT向命名空间添加前缀

使用XSLT向命名空间添加前缀,xslt,namespaces,Xslt,Namespaces,我需要在名称空间中添加前缀 输入XML: <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <ProductMovementReport Version="5.0" xmlns="urn:cidx:names:specification:ces:schema:all:5:0"> <Header>

我需要在名称空间中添加前缀

输入XML:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <ProductMovementReport Version="5.0" xmlns="urn:cidx:names:specification:ces:schema:all:5:0">
            <Header>
                <ThisDocumentIdentifier>
                    <DocumentIdentifier>868</DocumentIdentifier>
                </ThisDocumentIdentifier>
            </Header>

        </ProductMovementReport>
    </soapenv:Body>
我还需要更改我能够成功获得的Version='5'的值

下面是欲望输出

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <urn1:ProductMovementReport Version="4.0" xmlns:urn1="urn:cidx:names:specification:ces:schema:all:4:0">
            <urn1:Header>
                <urn1:ThisDocumentIdentifier>
                    <urn1:DocumentIdentifier>868</urn1:DocumentIdentifier>
                </urn1:ThisDocumentIdentifier>

            </urn1:Header>

        </urn1:ProductMovementReport>
    </soapenv:Body>
我已经为此编写了代码,除了名称空间部分没有得到更改外,其他一切都正常工作。我想我在模板匹配中遗漏了一些东西

代码:

这是链接,以防我遗漏了什么

除了名称空间部分,我非常接近于解决它

有人能告诉我哪里做错了吗?

您的match=*匹配匹配所有元素,而不管名称空间如何。尝试通过测试名称空间URI来缩小范围

XSLT1.0

输出

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="@*">
    <xsl:choose>
        <xsl:when test="local-name() = 'Version' ">
            <xsl:attribute name="{local-name()}"><xsl:value-of select="'4.0'"/></xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="*">
    <xsl:element name="urn1:{local-name()}" namespace="urn:cidx:names:specification:ces:schema:all:4:0">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="*[namespace-uri()='urn:cidx:names:specification:ces:schema:all:5:0']">
        <xsl:element name="urn1:{local-name()}" namespace="urn:cidx:names:specification:ces:schema:all:4:0">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@Version">
        <xsl:attribute name="{name()}">
            <xsl:text>4.0</xsl:text>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>
<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <urn1:ProductMovementReport xmlns:urn1="urn:cidx:names:specification:ces:schema:all:4:0" Version="4.0">
      <urn1:Header>
         <urn1:ThisDocumentIdentifier>
            <urn1:DocumentIdentifier>868</urn1:DocumentIdentifier>
         </urn1:ThisDocumentIdentifier>
      </urn1:Header>
   </urn1:ProductMovementReport>
</soapenv:Body>