代码之家  ›  专栏  ›  技术社区  ›  Jason

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

  •  1
  • Jason  · 技术社区  · 3 年前

    这是我的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。我尝试了一些在这里找到的解决方案,但以上是我最接近目标的解决方案。其他解决方案添加了一个额外的命名空间。

    1 回复  |  直到 3 年前
        1
  •  1
  •   michael.hor257k    3 年前

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

    <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>