我得到了一个XML,我转换成另一个XML,但在翻译之后,我得到了一些我想删除的具有空名称空间声明(xmlns=""
)的元素。删除元素中的空名称空间声明
其次我还想要在InterChangeHead
元素中声明xmlns:xsi
。
输入XML
<?xml version="1.0" encoding="UTF-8"?>
<PostenInterchange type="tag">
<heInterchangeType>PostenInterchange</heInterchangeType>
<heVersion>1.4-rev3</heVersion>
<heTestindicator>1</heTestindicator>
<InterChangeHead type="tag">
<heVersion>1</heVersion>
<heSenderid>SENDID</heSenderid>
<heRecipientid>RECIPID</heRecipientid>
<heXmlnsxsi>"http://www.w3.org/2001/XMLSchema-instance"</heXmlnsxsi>
<heXmlns>"posten.xsd"</heXmlns>
<Shipment type="tag">
<heShipmenttype>IMP</heShipmenttype>
<Shipper type="tag">
<name>Shipper</name>
</Shipper>
<Consignee type="tag">
<name>Consignee</name>
</Consignee>
<GoodsData type="tag">
<heSequencenumber>2</heSequencenumber>
<GrossWeight>0.000</GrossWeight>
<NetWeight>0.660</NetWeight>
</GoodsData>
</Shipment>
</InterChangeHead>
</PostenInterchange>
CURRENT XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xs fn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Copy local names -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
<!-- Rename elements beginning with "he" to elements without the "he" -->
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="substring(local-name(), 1, 2) = 'he'">
<xsl:element name="{substring(local-name(), 3)}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Correct InterChangeHead element -->
<xsl:template match="InterChangeHead">
<xsl:element name="InterChangeHead" namespace="posten.xsd">
<!-- Copy childs -->
<xsl:apply-templates select="child::node()" />
</xsl:element>
</xsl:template>
<!-- Remove type attribute -->
<xsl:template match="@type"/>
<!-- Remove unnecessary elements -->
<xsl:template match="heXmlnsxsi" />
<xsl:template match="heXmlns" />
</xsl:stylesheet>
电流输出
<?xml version="1.0" encoding="UTF-8"?>
<PostenInterchange>
<InterchangeType>PostenInterchange</InterchangeType>
<Version>1.4-rev3</Version>
<Testindicator>1</Testindicator>
<InterChangeHead xmlns="posten.xsd">
<Version xmlns="">1</Version>
<Senderid xmlns="">SENDID</Senderid>
<Recipientid xmlns="">RECIPID</Recipientid>
<Shipment xmlns="">
<Shipmenttype>IMP</Shipmenttype>
<Shipper>
<name>Shipper</name>
</Shipper>
<Consignee>
<name>Consignee</name>
</Consignee>
<GoodsData>
<Sequencenumber>2</Sequencenumber>
<GrossWeight>0.000</GrossWeight>
<NetWeight>0.660</NetWeight>
</GoodsData>
</Shipment>
</InterChangeHead>
</PostenInterchange>
所需的输出
<?xml version="1.0" encoding="UTF-8"?>
<PostenInterchange>
<InterchangeType>PostenInterchange</InterchangeType>
<Version>1.4-rev3</Version>
<Testindicator>1</Testindicator>
<InterChangeHead xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="posten.xsd" >
<Version>1</Version>
<Senderid>SENDID</Senderid>
<Recipientid>RECIPID</Recipientid>
<Shipment>
<Shipmenttype>IMP</Shipmenttype>
<Shipper>
<name>Shipper</name>
</Shipper>
<Consignee>
<name>Consignee</name>
</Consignee>
<GoodsData>
<Sequencenumber>2</Sequencenumber>
<GrossWeight>0.000</GrossWeight>
<NetWeight>0.660</NetWeight>
</GoodsData>
</Shipment>
</InterChangeHead>
</PostenInterchange>
谁能帮助我的最后一位?我加了一个模板,因为我认为这将删除空的命名空间声明:
<!-- Copy local names -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
为什么template''需要? –
2013-04-08 11:58:22
@MarkVeenstra我从技术上来说并不是因为你正在处理的XML输入没有任何属性(除了你无视的'标签'之外)以及该模板在'text()节点与默认相同。习惯,我认为,我总是倾向于从身份模板开始...... – 2013-04-08 12:01:55