感谢 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())]"/>
删除不在无命名空间中的任何属性。
关于xml - 使用 XSLT 从 XML 中的选择性节点中删除选择性附加命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40937963/