-1

I want to convert below XML

<Root>
  <instruction>
    <header>
      <type>A</type>
      <date>29-08-2018</date>
    </header>
    <message>
      <name>parth</name>
      <age>24</age>
    </message>
  </instruction>
</Root>

To below XML using XSLT

<Root>
  <request>
    <instruction>
      <header>
        <type>A</type>
        <date>29-08-2018</date>
      </header>
      <message>
        <name>parth</name>
        <age>24</age>
      </message>
    </instruction>
  </request>
</Root>

In above output all the tags inside <request> tag are in form of string, not XML elements. Any advice?

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • There are more fields in original file need to have the whole xml structure in output map. please advice. – Parth Ajmani Aug 29 '18 at 12:46
  • Your input XML is not well formed, as it has an opening `` tag, but no closing ``. Can you correct the XML in your question, otherwise it won't be possible to apply XSLT to it. Can you also say what logic you are trying to implement too? Are you just trying to add the `request` node? – Tim C Aug 29 '18 at 12:51
  • Good Day Tim i checked and corrected it please have a look now – Parth Ajmani Aug 29 '18 at 12:52
  • 1
    I was referring to the input XML, which is missing the end tag. Also, can you confirm all you are trying to do is add the `request` tag to the XML? Thank you – Tim C Aug 29 '18 at 13:03
  • You also say "the tags inside `` tag are in form of string". Do you mean you want to "escape" all the nodes in this case, or output them as CDATA. If this is the case, you should show the actual XML you want. Thank you. – Tim C Aug 29 '18 at 13:28
  • yes inside request tag in destination xml are all string including source schema xml tags. – Parth Ajmani Aug 29 '18 at 13:33
  • Good Day Tim, I'm new to xslt so doesn't know much. please advice for above query. – Parth Ajmani Aug 29 '18 at 13:59

1 Answers1

1

Taking what is shown in your question, I would write the XSLT like so:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />

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

  <xsl:template match="Root">
    <xsl:copy>
      <request>
        <xsl:apply-templates />
      </request>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This outputs the XML as shown in your question. See http://xsltfiddle.liberty-development.net/pPqsHTL

Note the use of the identity template in copying existing elements.

However, from your comments it sounds you want to convert the elements to text, which is achieved by "escaping" them. If this is the case, I would write the XSLT like this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" cdata-section-elements="request" />

  <xsl:template match="*">
    <xsl:value-of select="concat('&lt;', name())" />
    <xsl:for-each select="@*">
      <xsl:value-of select="concat(' ', name(), '=&quot;', ., '&quot;')" />
    </xsl:for-each>
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates />
    <xsl:value-of select="concat('&lt;/', name(), '&gt;')" />
  </xsl:template>

  <xsl:template match="Root">
    <xsl:copy>
      <request>
        <xsl:apply-templates />
      </request>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

This outputs the following

<Root>
   <request><![CDATA[
  <instruction a="1">
    <header>
      <type>A</type>
      <date>29-08-2018</date>
    </header>
    <message>
      <name>parth</name>
      <age>24</age>
    </message>
  </instruction>
]]></request>
</Root>

See http://xsltfiddle.liberty-development.net/nc4NzQJ

Tim C
  • 70,053
  • 14
  • 74
  • 93
  • Thanks for the solution tim. When itry to apply this inside BizTalk scripting functoid im getting some error. Please find below the request xml(source)
    Age
    CRef
    – Parth Ajmani Aug 31 '18 at 15:27
  • Error 3 XSL transform error: 'xsl:template' cannot be a child of the 'xsl:template' element. – Parth Ajmani Aug 31 '18 at 15:32
  • I am afraid I don't know BizTalk, so don't know anything about how to apply XSLT using it. – Tim C Aug 31 '18 at 15:48
  • can you please advise on the error. That might help – Parth Ajmani Aug 31 '18 at 16:33
  • I think i might know why it is giving error Can you please advise me how to do this transformation if source xml target namespace is "http://tempuri.com" and destination xml namespace is "http:gootemp.com" – Parth Ajmani Aug 31 '18 at 16:50
  • It would help if you if you edited you question to show the XML with the namespaces. Thank you. – Tim C Aug 31 '18 at 19:24