输入XML -
<?xml version="1.0"?>
<EsrOrder>
<Header>
<OrderId>
865975
</OrderId>
<Metadata>
None
</Metadata>
</Header>
<ESRBody>
<TN>
6543219785
</TN>
<Tntype>
Wireline
</Tntype>
<Priority>
High
</Priority>
<TnOwnerName>
Ching Chang
</TnOwnerName>
<TnAddress>
<BasicAddress>
101,Clssic View 44 Baikunth Dham Colony
</BasicAddress>
<Landmark>
Near Anand Bazar
</Landmark>
<City>
Indore
</City>
<State>
Madhya Pradesh
</State>
<Country>
India
</Country>
</TnAddress>
</ESRBody>
</EsrOrder>
XSLT USED -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:copy>
<Sea_Element>
<xsl:apply-templates/>
</Sea_Element>
</xsl:copy>
</xsl:template>
<xsl:template match="EsrOrder/Header">
<xsl:copy-of select="@*|node()" />
</xsl:template>
<xsl:template match="EsrOrder/ESRBody">
<xsl:copy-of select="@*|node()" />
</xsl:template>
<!--<xsl:template match="EsrOrder/ESRBody/TnAddress">
<xsl:copy-of select="@*|node()" />
</xsl:template>-->
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>
我得到的输出 -
<?xml version="1.0" encoding="UTF-8"?>
<Sea_Element>
<OrderId>
865975
</OrderId>
<Metadata>
None
</Metadata>
<TN>
6543219785
</TN>
<Tntype>
Wireline
</Tntype>
<Priority>
High
</Priority>
<TnOwnerName>
Ching Chang
</TnOwnerName>
<TnAddress>
<BasicAddress>
101,Clssic View 44 Baikunth Dham Colony
</BasicAddress>
<Landmark>
Near Anand Bazar
</Landmark>
<City>
Indore
</City>
<State>
Madhya Pradesh
</State>
<Country>
India
</Country>
</TnAddress>
</Sea_Element>
需要输出的地方 -
<?xml version="1.0" encoding="UTF-8"?>
<Sea_Element>
<OrderId>
865975
</OrderId>
<Metadata>
None
</Metadata>
<TN>
6543219785
</TN>
<Tntype>
Wireline
</Tntype>
<Priority>
High
</Priority>
<TnOwnerName>
Ching Chang
</TnOwnerName>
<BasicAddress>
101,Clssic View 44 Baikunth Dham Colony
</BasicAddress>
<Landmark>
Near Anand Bazar
</Landmark>
<City>
Indore
</City>
<State>
Madhya Pradesh
</State>
<Country>
India
</Country>
</Sea_Element>
它只是一个未被消除的父字段。 即使相同的代码正在用于其他标记以进行删除 我能得到理由并纠正XSL ....
答案 0 :(得分:1)
问题在于使用copy-of
会阻止应用与TnAddress
等内部元素匹配的任何模板。
试试这个更简单的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Elements that should be dropped - copying only their content -->
<xsl:template match="EsrOrder|Header|ESRBody|TnAddress">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
<!-- Set the root element -->
<xsl:template match="/">
<xsl:copy>
<Sea_Element>
<xsl:apply-templates/>
</Sea_Element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>