这是我的 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 添加到另一个元素。我尝试了在这里找到的一些解决方案,但上面的解决方案是我最接近目标的。其他解决方案添加了额外的命名空间。
要获得显示的确切结果,您必须将旧命名空间中的所有元素和属性移动到新命名空间中: