0

Been trying all day to simply add an xmlns attribute to the root element of my xml, but can't seem to get it right.

Source = 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>

Desired result = 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>

My best attempt to achieve this result is this 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>

This is the result.
Not entirely correct, because it seems to self-close the <INPUTS> tag, while I'd like it to appear on the last line of the output:

<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>

I've succeeded to remove this line

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

However, I just cannot seem to add the xmlns="GE_Schemas" attribute to the root <INPUTS> tag.

It's also important to keep the tags NOT self-closing.
So <Zone></Zone> must stay that way, and cannot be changed to <Zone/> as is also the case right now.

Does anyone have an idea how to solve these 2 issues?

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42

1 Answers1

0

Use this transformation

<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>

Note: according to the xml specification, the <Zone></Zone> and <Zone/> nodes are equivalent.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Hi Alexander, thanks, this works, but it still has 1 small problem. The empty child-tags are still being self-closed. For example, it outputs `` while it should be `` – Bjorn Mistiaen Jun 19 '20 at 14:47
  • @BjornMistiaen - No, it shouldn't. Any xml parser will perceive nodes `` and `` the same way. – Alexander Petrov Jun 19 '20 at 14:51
  • Yes, I know they're semantically the same, and any xml parser should be able to handle them, but unfortunately we're working with a 3rd party vendor whose software can't handle self-closing tags... – Bjorn Mistiaen Jun 19 '20 at 14:53
  • Which XSLT engine are you using for you XSLT transformation? – Sebastien Jun 19 '20 at 14:54
  • Since the 3rd party software uses a non-standard parsing method, maybe they also strip the spaces, so you could try to add this line right after the xsl:apply-templates : . This would add a space to empty elements and the closing node would be shown. – Sebastien Jun 19 '20 at 15:05
  • @Sebastien I don't know which engine is used. How can I see that? – Bjorn Mistiaen Jun 19 '20 at 15:06
  • @BjornMistiaen Refer to this SO answer to find out which enginer/version you are dealing with : https://stackoverflow.com/questions/25244370/how-can-i-check-which-xslt-processor-is-being-used-in-solr – Sebastien Jun 19 '20 at 15:10
  • @Sebastien I'm using Altova XMLSpy for testing this. Vendor = Altova GmbH and version = 1 – Bjorn Mistiaen Jun 19 '20 at 15:15
  • 2
    Note that this will remove any processing instructions or comments in the input document. Not an issue here but if you plan to apply this generally you might want to be aware of that. – Nic Gibson Jun 19 '20 at 15:31
  • @BjornMistiaen - To retain the end tags, try using "html" as the output method...http://xsltfiddle.liberty-development.net/pNmCzsE/1 – Daniel Haley Jun 19 '20 at 16:06
  • @DanielHaley Thanks, already tried that. It still self-closes the tags. – Bjorn Mistiaen Jun 19 '20 at 16:09