将 xmlns 属性添加到根元素



一整天都在尝试简单地将xmlns属性添加到我的xml的根元素中,但似乎无法正确。

源 = A.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<INPUTS>
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority></Priority>
<LineNbr>VPI000594422</LineNbr>
<Article>B02369</Article>
<Description>AANSLUITKLEM</Description>
<Quantity>14,00</Quantity>
<Location></Location>
<LotNbr></LotNbr>
<Comment></Comment>
<Zone>test</Zone>
<InboundCarrier></InboundCarrier>
<UnitOfMeasurement></UnitOfMeasurement>
</INP_NO_PARAM>
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority></Priority>
<LineNbr>VPI000594426</LineNbr>
<Article>B08432</Article>
<Description>AARDINGSKLEM</Description>
<Quantity>321,00</Quantity>
<Location></Location>
<LotNbr></LotNbr>
<Comment></Comment>
<Zone></Zone>
<InboundCarrier></InboundCarrier>
<UnitOfMeasurement></UnitOfMeasurement>
</INP_NO_PARAM>
</INPUTS>

期望结果 = B.xml

<INPUTS xmlns="GE_Schemas">
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority></Priority>
<LineNbr>VPI000594422</LineNbr>
<Article>B02369</Article>
<Description>AANSLUITKLEM</Description>
<Quantity>14,00</Quantity>
<Location></Location>
<LotNbr></LotNbr>
<Comment></Comment>
<Zone></Zone>
<InboundCarrier></InboundCarrier>
<UnitOfMeasurement></UnitOfMeasurement>
</INP_NO_PARAM>
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority></Priority>
<LineNbr>VPI000594426</LineNbr>
<Article>B08432</Article>
<Description>AARDINGSKLEM</Description>
<Quantity>321,00</Quantity>
<Location></Location>
<LotNbr></LotNbr>
<Comment></Comment>
<Zone></Zone>
<InboundCarrier></InboundCarrier>
<UnitOfMeasurement></UnitOfMeasurement>
</INP_NO_PARAM>
</INPUTS>

我实现此结果的最佳尝试是以下 xsd:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<!--No xml declaration line-->
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<!-- copy everything as-is except for more specific templates below -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<!--Change INPUTS tag-->
<xsl:template match="INPUTS">
<INPUTS xmlns="GE_Schemas" >
</INPUTS>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>

这就是结果。
不完全正确,因为它似乎可以自行关闭<INPUTS>标签,而我希望它出现在输出的最后一行:

<INPUTS xmlns="GE_Schemas"/>
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority/>
<LineNbr>VPI000594422</LineNbr>
<Article>B02369</Article>
<Description>AANSLUITKLEM</Description>
<Quantity>14,00</Quantity>
<Location/>
<LotNbr/>
<Comment/>
<Zone>test</Zone>
<InboundCarrier/>
<UnitOfMeasurement/>
</INP_NO_PARAM>
<INP_NO_PARAM>
<OrderNbr>TR-00001541</OrderNbr>
<Priority/>
<LineNbr>VPI000594426</LineNbr>
<Article>B08432</Article>
<Description>AARDINGSKLEM</Description>
<Quantity>321,00</Quantity>
<Location/>
<LotNbr/>
<Comment/>
<Zone/>
<InboundCarrier/>
<UnitOfMeasurement/>
</INP_NO_PARAM>

我已经成功地删除了这一行

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

但是,我似乎无法将xmlns="GE_Schemas"属性添加到根<INPUTS>标签中。

保持标签不自动关闭也很重要。
因此,<Zone></Zone>必须保持这种状态,不能像现在这样更改为<Zone/>

有人知道如何解决这两个问题吗?

使用此转换

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="GE_Schemas">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

注意:根据 xml 规范,<Zone></Zone><Zone/>节点是等效的。

最新更新