我想使用XSLT从xml中删除命名空间和父节点。
下面提到的是源码和目标xml.kindly帮助我。
****Source.xml****
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:completeProductionPlan xmlns="http://ServiceManagement/OIS_Services_v01.00/common"
xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types">
<ns2:messageID>
<value>9133235059913398501_9133235059913398860</value>
</ns2:messageID>
</ns2:completeProductionPlan>
</soapenv:Body>
</soapenv:Envelope>
****Target****
<completeProductionPlan >
<MessageId>9133235059913398501_9133235059913398860</MessageId>
</completeProductionPlan>
namespace_Remove.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
答案 0 :(得分:0)
到目前为止,您使用的XSLT将从XML中删除命名空间,但不会更改结构。为此,您需要添加一些额外的模板,记住样式表中的匹配和选择表达式必须针对具有命名空间的原始输入XML进行操作,而不是您正在生成的无命名空间版本。
首先,您需要向xsl:stylesheet
添加一些名称空间声明:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns2="http://ServiceManagement/TechnicalOrderManagement/ProductionFulfillment_v01.00/types"
xmlns:common="http://ServiceManagement/OIS_Services_v01.00/common"
exclude-result-prefixes="soap ns2 common">
现在您可以添加合适的模板:
<xsl:template match="/">
<!-- ignore everything except the first child element of the Body -->
<xsl:apply-templates select="soap:Envelope/soap:Body/*[1]" />
</xsl:template>
<xsl:template match="ns2:messageID">
<MessageID><xsl:value-of select="common:value" /></MessageID>
</xsl:template>
当然,对于这个特定的例子,你可以使用一个更简单的样式表结构,在一个模板中硬编码输出格式,只是从原始XML中提取消息ID值,但最好习惯更多当您开始需要处理更复杂的格式时,通用的XSLT编程风格。