使用 XSLT 从 XML 中的选择性节点中删除选择性附加命名空间
Remove selective additional namespace from selective nodes in XML using XSLT
感谢 Stack Overflow 和@michael.hor257k,我能够在我的 XML 文档中成功更新命名空间。
现在,只有一个问题。
我的 SAP 系统无法理解 XSD 中的几个字段,因此添加了自己的命名空间以适应 XML 架构。
这可以通过 SAP 解决,但我们还没有安装该软件 (SPROXY)。
所以,我必须使用 XSLT 1.0 来实现。
我原来的XML是:
<?xml version="1.0" encoding="UTF-8"?>
<n0:eCPR xmlns:prx="urn:sap.com:proxy:DV4:/1SAI/TAS1F59A417878D36573F1D:700:2013/05/24" xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<n0:employees>
<n0:employee>
<n0:name id="20019768">Paul John</n0:name>
</n0:employee>
</n0:employees>
</n0:eCPR>
以及用于实现以下所需输出的 XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="CPR:{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
使用当前命名空间更新后我的XML(错误所在)看起来像
<?xml version="1.0" encoding="UTF-8"?>
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<CPR:projectInfo>
<CPR:awardingBody xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:contractAgencyID xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:contractAgency/>
<CPR:projectName xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:projectID/>
<CPR:awardingBodyID/>
<CPR:projectNum/>
<CPR:contractID/>
因此,节点awardingBody
、contractAgencyID
和Project Name
是系统无法正确转换的字段。(具有xmlns:asx
命名空间的节点)
我需要删除这些命名空间。是否可以通过 XSLT。条件:仅删除命名空间为 http://www.sap.com/abapxml 的那些条目的命名空间,或者我可以提供节点名称(因为它们是固定的)
理想的结构应该是什么:
<?xml version="1.0" encoding="UTF-8"?>
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<CPR:projectInfo>
<CPR:awardingBody/>
<CPR:contractAgencyID />
<CPR:contractAgency/>
<CPR:projectName/>
<CPR:projectID/>
<CPR:awardingBodyID/>
<CPR:projectNum/>
<CPR:contractID/>
谢谢
我猜 (!) 你原来的 XML 实际上看起来像这样:
XML
<n0:eCPR xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd" xmlns:asx="http://www.sap.com/abapxml">
<n0:projectInfo>
<n0:awardingBody asx:root=""/>
<n0:contractAgencyID asx:root=""/>
<n0:contractAgency/>
<n0:projectName asx:root=""/>
<n0:projectID/>
<n0:awardingBodyID/>
<n0:projectNum/>
<n0:contractID/>
</n0:projectInfo>
</n0:eCPR>
为了获得请求的输出——即不复制 asx:root
属性——你需要做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="CPR:{local-name()}">
<xsl:copy-of select="@*[not(namespace-uri()='http://www.sap.com/abapxml')]"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
或者也许这样做:
<xsl:copy-of select="@*[not(namespace-uri())]"/>
删除任何 不在无命名空间中的属性。
感谢 Stack Overflow 和@michael.hor257k,我能够在我的 XML 文档中成功更新命名空间。
现在,只有一个问题。
我的 SAP 系统无法理解 XSD 中的几个字段,因此添加了自己的命名空间以适应 XML 架构。
这可以通过 SAP 解决,但我们还没有安装该软件 (SPROXY)。
所以,我必须使用 XSLT 1.0 来实现。
我原来的XML是:
<?xml version="1.0" encoding="UTF-8"?>
<n0:eCPR xmlns:prx="urn:sap.com:proxy:DV4:/1SAI/TAS1F59A417878D36573F1D:700:2013/05/24" xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<n0:employees>
<n0:employee>
<n0:name id="20019768">Paul John</n0:name>
</n0:employee>
</n0:employees>
</n0:eCPR>
以及用于实现以下所需输出的 XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="CPR:{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
使用当前命名空间更新后我的XML(错误所在)看起来像
<?xml version="1.0" encoding="UTF-8"?>
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<CPR:projectInfo>
<CPR:awardingBody xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:contractAgencyID xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:contractAgency/>
<CPR:projectName xmlns:asx="http://www.sap.com/abapxml" asx:root=""/>
<CPR:projectID/>
<CPR:awardingBodyID/>
<CPR:projectNum/>
<CPR:contractID/>
因此,节点awardingBody
、contractAgencyID
和Project Name
是系统无法正确转换的字段。(具有xmlns:asx
命名空间的节点)
我需要删除这些命名空间。是否可以通过 XSLT。条件:仅删除命名空间为 http://www.sap.com/abapxml 的那些条目的命名空间,或者我可以提供节点名称(因为它们是固定的)
理想的结构应该是什么:
<?xml version="1.0" encoding="UTF-8"?>
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<CPR:projectInfo>
<CPR:awardingBody/>
<CPR:contractAgencyID />
<CPR:contractAgency/>
<CPR:projectName/>
<CPR:projectID/>
<CPR:awardingBodyID/>
<CPR:projectNum/>
<CPR:contractID/>
谢谢
我猜 (!) 你原来的 XML 实际上看起来像这样:
XML
<n0:eCPR xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd" xmlns:asx="http://www.sap.com/abapxml">
<n0:projectInfo>
<n0:awardingBody asx:root=""/>
<n0:contractAgencyID asx:root=""/>
<n0:contractAgency/>
<n0:projectName asx:root=""/>
<n0:projectID/>
<n0:awardingBodyID/>
<n0:projectNum/>
<n0:contractID/>
</n0:projectInfo>
</n0:eCPR>
为了获得请求的输出——即不复制 asx:root
属性——你需要做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="CPR:{local-name()}">
<xsl:copy-of select="@*[not(namespace-uri()='http://www.sap.com/abapxml')]"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
或者也许这样做:
<xsl:copy-of select="@*[not(namespace-uri())]"/>
删除任何 不在无命名空间中的属性。