将xmlns属性添加到根元素

时间:2020-06-19 14:21:19

标签: xml xslt

一整天都想向我的xml的根元素中添加一个xmlns属性,但似乎无法正确处理。

源= 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/>

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

1 个答案:

答案 0 :(得分:0)

使用此转换

<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/>节点是等效的。

相关问题