我正在尝试删除除soapenv之外的所有名称空间:。
以下是我正在使用的代码。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
<!-- Stylesheet to remove all namespaces from a document -->
<!-- NOTE: this will lead to attribute name clash, if an element contains
two attributes with same local name but different namespace prefix -->
<!-- Nodes that cannot have a namespace are copied as such -->
<!-- template to copy elements -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<!-- template to copy attributes -->
<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
<!-- template to copy the rest of the nodes -->
<xsl:template match="comment() | text() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
输入XML
<soapenv:Envelope xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns0:ShipNotice xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="urn:cidx:names:specification:ces:schema:all:5:0" Version="5.0">
<ns0:Header>
<ns0:ThisDocumentIdentifier>
<ns0:DocumentIdentifier>0000001053685572</ns0:DocumentIdentifier>
</ns0:ThisDocumentIdentifier>
<ns0:ThisDocumentDateTime>
<ns0:DateTime DateTimeQualifier="On">2014-07-21T12:16:28</ns0:DateTime>
</ns0:ThisDocumentDateTime>
<ns0:RequestingDocumentDateTime>
<ns0:DateTime DateTimeQualifier="On">2014-07-21T12:16:29</ns0:DateTime>
</ns0:RequestingDocumentDateTime>
</ns0:Header>
</ns0:ShipNotice>
</soapenv:Body>
</soapenv:Envelope>
代码输出
<Envelope>
<Header/>
<Body>
<ShipNotice Version="5.0">
<Header>
<ThisDocumentIdentifier>
<DocumentIdentifier>0000001053685572</DocumentIdentifier>
</ThisDocumentIdentifier>
<ThisDocumentDateTime>
<DateTime DateTimeQualifier="On">2014-07-21T12:16:28</DateTime>
</ThisDocumentDateTime>
<RequestingDocumentDateTime>
<DateTime DateTimeQualifier="On">2014-07-21T12:16:29</DateTime>
</RequestingDocumentDateTime>
</Header>
</ShipNotice>
</Body>
</Envelope>
这是我想要的输出。
<soapenv:Envelope xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ShipNotice Version="5.0">
<Header>
<ThisDocumentIdentifier>
<DocumentIdentifier>0000001053685572</DocumentIdentifier>
</ThisDocumentIdentifier>
<ThisDocumentDateTime>
<DateTime DateTimeQualifier="On">2014-07-21T12:16:28</DateTime>
</ThisDocumentDateTime>
<RequestingDocumentDateTime>
<DateTime DateTimeQualifier="On">2014-07-21T12:16:29</DateTime>
</RequestingDocumentDateTime>
</Header>
</ShipNotice>
</soapenv:Body>
</soapenv:Envelope>
有人可以告诉我如何实现这个目标吗?
更新
我添加了这个,它解决了我的问题。
<xsl:template match="soapenv:Envelope | soapenv:Header | soapenv:Body">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
答案 0 :(得分:2)
您需要添加此模板:
<xsl:template match="soapenv:*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
对于属性在soapenv:
命名空间中的不太可能发生的事件(并假设您希望将其保留在那里),还要添加:
<xsl:template match="@soapenv:*">
<xsl:copy/>
</xsl:template>