cancel
Showing results for 
Search instead for 
Did you mean: 

additional namespace and tags through xslt mapping

Former Member
0 Kudos

Hi Guys,

I need some assistance basically, I have a requirement in which I have to add additional tags in the output xml file.

INPUT

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

<StandardBusinessDocument>

  <StandardBusinessDocumentHeader>

    <HeaderVersion>str1234</HeaderVersion>

    <Sender>

      <Identifier Authority="str1234">str1234</Identifier>

    </Sender>

    <Receiver>

      <Identifier Authority="str1234">str1234</Identifier>

    </Receiver>

    <DocumentIdentification>

      <Standard>str1234</Standard>

      <TypeVersion>str1234</TypeVersion>

      <InstanceIdentifier>str1234</InstanceIdentifier>

      <Type>str1234</Type>

      <CreationDateAndTime>str1234</CreationDateAndTime>

    </DocumentIdentification>

    <BusinessScope>

  <Scope>

  <Type>str1234</Type>

  <InstanceIdentifier>str1234</InstanceIdentifier>

  <Identifier>str1234</Identifier>

  </Scope>

  </BusinessScope>

  </StandardBusinessDocumentHeader>

  <message>

    <entityIdentification>

      <uniqueCreatorIdentification>str1234</uniqueCreatorIdentification>

    </entityIdentification>

   </message>

</StandardBusinessDocument>

EXPECTED OUTPUT

<?xml version="1.0" encoding="UTF-8" ?>

<sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">

<sh:StandardBusinessDocumentHeader>

<sh:HeaderVersion>1.3</sh:HeaderVersion>

<sh:Sender>

<sh:Identifier Authority="GLN">0000000000</sh:Identifier>

</sh:Sender>

<sh:Receiver>

<sh:Identifier Authority="CODE">00000</sh:Identifier>

</sh:Receiver>

<sh:DocumentIdentification>

<sh:Standard>TEST</sh:Standard>

<sh:TypeVersion>1P</sh:TypeVersion>

<sh:InstanceIdentifier></sh:InstanceIdentifier>

<sh:Type>Notification</sh:Type>

<sh:CreationDateAndTime>2013-03-11T19:26:25 </sh:CreationDateAndTime>

</sh:DocumentIdentification>

<sh:BusinessScope>

<sh:Scope>

<sh:Type>111</sh:Type>

<sh:InstanceIdentifier>111</sh:InstanceIdentifier>

<sh:Identifier>345</sh:Identifier>

</sh:Scope>

</sh:BusinessScope>

</sh:StandardBusinessDocumentHeader>

<common:message>

<entityIdentification>

<uniqueCreatorIdentification>1234</uniqueCreatorIdentification>

</entityIdentification>

</common:message>

</sh:StandardBusinessDocument>

XSLT MAPPING

<?xml version='1.0' encoding='utf-8'?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

<sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">

<sh:StandardBusinessDocumentHeader>

<sh:HeaderVersion>

<xsl:value-of select="//HeaderVersion" />

</sh:HeaderVersion>

<sh:Sender>

<xsl:copy-of select="//Sender" />

</sh:Sender>

</sh:StandardBusinessDocumentHeader>

</sh:StandardBusinessDocument>

</xsl:template>

</xsl:stylesheet>

Can somebody please guide me as I am bit new to xslt. I have already tried on scn but no luck.

Regards,

Asif

Accepted Solutions (1)

Accepted Solutions (1)

iaki_vila
Active Contributor
0 Kudos

Hi Asif,

As Raghu said you have to set a namespce URI for message.

The first and second template set all the tags with the namespace sh. The last one template rename only the message tags and its children.

You can test this XSL as well:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/> 

<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">

     <xsl:copy>

       <xsl:apply-templates select="node()|@*"/>

     </xsl:copy>

</xsl:template>

<xsl:template match="*">

      <xsl:element name="sh:{name()}" namespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">

       <xsl:copy-of select="namespace::*"/>

           <xsl:apply-templates select="node()|@*"/>

  </xsl:element>

</xsl:template>

<xsl:template match="//message">

    <common:message xmlns:common="some__namespace">

        <xsl:copy-of select="*"/>

    </common:message>

</xsl:template>

</xsl:stylesheet>

Regards.

Former Member
0 Kudos

Thanks guys I have solved the problem myself and wrote the xslt which is giving me desired output.

engswee
Active Contributor
0 Kudos

Asif,

In the spirit of the community, would you kindly share the solution you came up with for the benefit of others in the future?

If Raghu's and Inaki's replies were not helpful in pointing you in the right direction, perhaps you can also share how your solution differed from theirs.

Rgds

Eng Swee

Answers (1)

Answers (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Asif,

<common:message> is not valid element name (it not well formed XML) it should be <common:message xmlns:common="SomeNamespace">. Please use below XSLT.


<?xml version='1.0' encoding='utf-8'?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

   

    <xsl:template match="StandardBusinessDocument">

        <sh:StandardBusinessDocument xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">

            <xsl:apply-templates/>

        </sh:StandardBusinessDocument>

    </xsl:template>

   

    <xsl:template match="StandardBusinessDocumentHeader">

        <xsl:copy-of select="." />

    </xsl:template>

   

    <xsl:template match="message">

        <common:message xmlns:common="SomeNamespace">

            <xsl:apply-templates/>

        </common:message>

    </xsl:template>

   

    <xsl:template match="entityIdentification">

        <xsl:copy-of select="." />

    </xsl:template>            

</xsl:stylesheet>