如何移除XSLT转换后SOAP Envelope中的多余wd命名空间?
问题
我尝试通过XSLT转换读取报表以生成SOAP请求,但XSLT始终会在SOAP Envelope中添加报表名称对应的wd命名空间(urn:com.workday.report/CR_INT433_Employee_Home_Address)。
我的XSLT代码如下:
<?xml version='1.0' encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday.report/CR_INT433_Employee_Home_Address" > <xsl:template match="/"> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <env:Body> <xsl:for-each select="wd:Report_Data/wd:Report_Entry"> <bsvc:Maintain_Contact_Information_for_Person_Event_Request xmlns:bsvc="urn:com.workday/bsvc" bsvc:Add_Only="true" bsvc:version="v43.0"> <bsvc:Business_Process_Parameters> <bsvc:Auto_Complete>true</bsvc:Auto_Complete> <bsvc:Run_Now>true</bsvc:Run_Now> </bsvc:Business_Process_Parameters> <bsvc:Maintain_Contact_Information_Data> <bsvc:Worker_Reference> <bsvc:ID bsvc:type="Employee_ID"> <xsl:value-of select="wd:Employee_ID"/> </bsvc:ID> </bsvc:Worker_Reference> <bsvc:Effective_Date> <xsl:value-of select="format-date(wd:Hire_Date, '[Y0001]-[M01]-[D01]')"/> </bsvc:Effective_Date> <bsvc:Worker_Contact_Information_Data> <bsvc:Address_Data bsvc:Do_Not_Replace_All="true"> <bsvc:Country_Reference> <bsvc:ID bsvc:type="ISO_3166-1_Alpha-3_Code"> <xsl:value-of select="wd:Primary_Home_Address/wd:Country"/> </bsvc:ID> </bsvc:Country_Reference> <bsvc:Address_Line_Data bsvc:Type="ADDRESS_LINE_1"> <xsl:value-of select="wd:Primary_Home_Address/wd:Address_Line_1"/> </bsvc:Address_Line_Data> <bsvc:Address_Line_Data bsvc:Type="ADDRESS_LINE_2"> <xsl:value-of select="wd:Primary_Home_Address/wd:Address_Line_2"/> </bsvc:Address_Line_Data> <bsvc:Municipality> <xsl:value-of select="wd:Primary_Home_Address/wd:City"/> </bsvc:Municipality> <bsvc:Country_Region_Reference> <bsvc:ID bsvc:type="ISO_3166-2_Code"> <xsl:value-of select="wd:Primary_Home_Address/wd:State"/> </bsvc:ID> </bsvc:Country_Region_Reference> <bsvc:Postal_Code> <xsl:value-of select="wd:Primary_Home_Address/wd:Postal"/> </bsvc:Postal_Code> <bsvc:Usage_Data bsvc:Public="0"> <bsvc:Type_Data> <bsvc:Type_Reference bsvc:Primary="1"> <bsvc:ID bsvc:type="WID">1f27f250dfaa4724ab1e1617174281e4</bsvc:ID> <bsvc:ID bsvc:type="Communication_Usage_Type_ID">WORK</bsvc:ID> </bsvc:Type_Reference> </bsvc:Type_Data> </bsvc:Usage_Data> </bsvc:Address_Data> </bsvc:Worker_Contact_Information_Data> </bsvc:Maintain_Contact_Information_Data> </bsvc:Maintain_Contact_Information_for_Person_Event_Request> </xsl:for-each> </env:Body> </env:Envelope> </xsl:template> </xsl:stylesheet>
当前输出的SOAP Envelope:
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wd="urn:com.workday.report/CR_INT433_Employee_Home_Address" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <!-- 请求内容 --> </env:Body> </env:Envelope>
期望的SOAP Envelope:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc"> <env:Body> <!-- 请求内容 --> </env:Body> </env:Envelope>
请问如何移除转换后Envelope中的xmlns:wd="urn:com.workday.report/CR_INT433_Employee_Home_Address"命名空间?
解决方案
核心修改逻辑
XSLT会自动将样式表中声明的、用于匹配源节点的命名空间复制到输出根元素,通过exclude-result-prefixes属性可以指定不输出该命名空间;同时直接在目标Envelope上声明你需要的wd命名空间即可。
修改后的XSLT代码
<?xml version='1.0' encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:com.workday.report/CR_INT433_Employee_Home_Address" exclude-result-prefixes="wd"> <!-- 指定不输出报表的wd命名空间 --> <xsl:template match="/"> <!-- 声明期望的wd命名空间,移除不需要的xsd命名空间 --> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wd="urn:com.workday/bsvc"> <env:Body> <xsl:for-each select="wd:Report_Data/wd:Report_Entry"> <bsvc:Maintain_Contact_Information_for_Person_Event_Request xmlns:bsvc="urn:com.workday/bsvc" bsvc:Add_Only="true" bsvc:version="v43.0"> <bsvc:Business_Process_Parameters> <bsvc:Auto_Complete>true</bsvc:Auto_Complete> <bsvc:Run_Now>true</bsvc:Run_Now> </bsvc:Business_Process_Parameters> <bsvc:Maintain_Contact_Information_Data> <bsvc:Worker_Reference> <bsvc:ID bsvc:type="Employee_ID"> <xsl:value-of select="wd:Employee_ID"/> </bsvc:ID> </bsvc:Worker_Reference> <bsvc:Effective_Date> <xsl:value-of select="format-date(wd:Hire_Date, '[Y0001]-[M01]-[D01]')"/> </bsvc:Effective_Date> <bsvc:Worker_Contact_Information_Data> <bsvc:Address_Data bsvc:Do_Not_Replace_All="true"> <bsvc:Country_Reference> <bsvc:ID bsvc:type="ISO_3166-1_Alpha-3_Code"> <xsl:value-of select="wd:Primary_Home_Address/wd:Country"/> </bsvc:ID> </bsvc:Country_Reference> <bsvc:Address_Line_Data bsvc:Type="ADDRESS_LINE_1"> <xsl:value-of select="wd:Primary_Home_Address/wd:Address_Line_1"/> </bsvc:Address_Line_Data> <bsvc:Address_Line_Data bsvc:Type="ADDRESS_LINE_2"> <xsl:value-of select="wd:Primary_Home_Address/wd:Address_Line_2"/> </bsvc:Address_Line_Data> <bsvc:Municipality> <xsl:value-of select="wd:Primary_Home_Address/wd:City"/> </bsvc:Municipality> <bsvc:Country_Region_Reference> <bsvc:ID bsvc:type="ISO_3166-2_Code"> <xsl:value-of select="wd:Primary_Home_Address/wd:State"/> </bsvc:ID> </bsvc:Country_Region_Reference> <bsvc:Postal_Code> <xsl:value-of select="wd:Primary_Home_Address/wd:Postal"/> </bsvc:Postal_Code> <bsvc:Usage_Data bsvc:Public="0"> <bsvc:Type_Data> <bsvc:Type_Reference bsvc:Primary="1"> <bsvc:ID bsvc:type="WID">1f27f250dfaa4724ab1e1617174281e4</bsvc:ID> <bsvc:ID bsvc:type="Communication_Usage_Type_ID">WORK</bsvc:ID> </bsvc:Type_Reference> </bsvc:Type_Data> </bsvc:Usage_Data> </bsvc:Address_Data> </bsvc:Worker_Contact_Information_Data> </bsvc:Maintain_Contact_Information_Data> </bsvc:Maintain_Contact_Information_for_Person_Event_Request> </xsl:for-each> </env:Body> </env:Envelope> </xsl:template> </xsl:stylesheet>
关键说明
exclude-result-prefixes="wd":告诉XSLT处理器,不要将样式表中用于匹配报表节点的wd命名空间输出到结果XML中。- 在
env:Envelope上声明xmlns:wd="urn:com.workday/bsvc":直接添加你期望的wd命名空间,满足输出格式要求。 - 移除
xsd命名空间:原代码中声明的xsd命名空间在期望输出中不存在,直接删除即可。
内容的提问来源于stack exchange,提问作者Sunny
相关产品推荐
相关产品推荐

