XSL 替换单个元素的命名空间 URI,但保留前缀

问题描述 投票:0回答:1

这是我的 XML:

<?xml version='1.0' encoding='utf-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <wd:Unpost_Job_Request
            xmlns:wd="urn:com.test.report"
            wd:version="v40.2">
            <wd:Business_Process_Parameters>
                <wd:Auto_Complete>true</wd:Auto_Complete>
                <wd:Run_Now>true</wd:Run_Now>
                <wd:Discard_On_Exit_Validation_Error>true</wd:Discard_On_Exit_Validation_Error>
                <wd:Comment_Data>
                    <wd:Comment>Unposting Triggered by Automation</wd:Comment>
                </wd:Comment_Data>
            </wd:Business_Process_Parameters>
            <wd:Unpost_Job_Data>
                <wd:Job_Posting_Reference>
                    <wd:ID wd:type="Job_Posting_ID">JOB_POSTING-3-23427</wd:ID>
                </wd:Job_Posting_Reference>
            </wd:Unpost_Job_Data>
        </wd:Unpost_Job_Request>
    </env:Body>
</env:Envelope>

我需要复制整个文档,只更改以下命名空间:

<wd:Unpost_Job_Request
xmlns:wd="urn:com.test.report" <-- Changed to xmlns:wd="urn:com.final.report"
wd:version="v40.2">

所以最终的预期输出是:

<?xml version='1.0' encoding='utf-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <wd:Unpost_Job_Request
            xmlns:wd="urn:com.final.report"
            wd:version="v40.2">
            <wd:Business_Process_Parameters>
                <wd:Auto_Complete>true</wd:Auto_Complete>
                <wd:Run_Now>true</wd:Run_Now>
                <wd:Discard_On_Exit_Validation_Error>true</wd:Discard_On_Exit_Validation_Error>
                <wd:Comment_Data>
                    <wd:Comment>Unposting Triggered by Automation</wd:Comment>
                </wd:Comment_Data>
            </wd:Business_Process_Parameters>
            <wd:Unpost_Job_Data>
                <wd:Job_Posting_Reference>
                    <wd:ID wd:type="Job_Posting_ID">JOB_POSTING-3-23427</wd:ID>
                </wd:Job_Posting_Reference>
            </wd:Unpost_Job_Data>
        </wd:Unpost_Job_Request>
    </env:Body>
</env:Envelope>

这是我当前的 XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wd="urn:com.test.report"
    exclude-result-prefixes="xs" version="2.0">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>

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

        </xsl:copy>
    </xsl:template>

    <xsl:template match="wd:Unpost_Job_Request">
        <xsl:element name="{local-name(.)}">
            <xsl:namespace name="wd">urn:com.final.report</xsl:namespace>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

返回:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body>
      <Unpost_Job_Request xmlns:wd="urn:com.final.report"
                          xmlns:wd_1="urn:com.test.report"
                          wd_1:version="v40.2">
         <wd:Business_Process_Parameters xmlns:wd="urn:com.test.report">
            <wd:Auto_Complete>true</wd:Auto_Complete>
            <wd:Run_Now>true</wd:Run_Now>
            <wd:Discard_On_Exit_Validation_Error>true</wd:Discard_On_Exit_Validation_Error>
            <wd:Comment_Data>
               <wd:Comment>Unposting Triggered by Automation</wd:Comment>
            </wd:Comment_Data>
         </wd:Business_Process_Parameters>
         <wd:Unpost_Job_Data xmlns:wd="urn:com.test.report">
            <wd:Job_Posting_Reference>
               <wd:ID wd:type="Job_Posting_ID">JOB_POSTING-3-23427</wd:ID>
            </wd:Job_Posting_Reference>
         </wd:Unpost_Job_Data>
      </Unpost_Job_Request>
   </env:Body>
</env:Envelope>

因此,它不是替换现有的 xmlns:wd URI,而是添加一个新的命名空间并将“_1”附加到旧的命名空间,然后将不需要的 xmlns:wd 添加到另一个元素。我尝试了在这里找到的一些解决方案,但以上是我最接近目标的解决方案。其他解决方案添加了额外的命名空间。

xml xslt xslt-2.0
1个回答
1
投票

要获得显示的确切结果,您必须将旧命名空间中的所有元素和属性移动到新命名空间中:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="urn:com.test.report"
xmlns:wd="urn:com.final.report"
exclude-result-prefixes="old">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="old:*" >
    <xsl:element name="wd:{local-name(.)}">
        <xsl:apply-templates select="node() | @*"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@old:*">
    <xsl:attribute name="wd:{local-name(.)}" select = "."/>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.