0

Below is my requirement ,

Input XML :

<ns0:TestDTM xmlns:ns0="http://SchemaIN">
  <ID>1234</ID>
  <Ref>A1</Ref>
  <Detail>
    <Qualifier>Q1</Qualifier>
    <Value>Name</Value>
  </Detail>
    <Detail>
    <Qualifier>Q2</Qualifier>
    <Value>Address</Value>
  </Detail>
  <Detail>
    <Qualifier>Q3</Qualifier>
    <Value>AddressType</Value>
  </Detail>
</ns0:TestDTM> 

Desired OutPut : Header Record will repeat only once

<ns0:Root xmlns:ns0="http://SchemaOUT">
  <Header>
    <ID>1234</ID>
    <Ref>A1</Ref>
    <Value1>Name</Value1>
    <Value2>Address</Value2>
    <Value3>AddressType</Value2>
  </Header>
</ns0:Root>

XSLT 1.0 Written :

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"  version="1.0" xmlns:s0="http://SchemaIN" xmlns:ns0="http://SchemaOUT">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/s0:TestDTM" />
  </xsl:template>
  <xsl:template match="/s0:TestDTM">
    <ns0:Root>
      <xsl:for-each select="Detail">
        <Header>
          <xsl:choose>
            <xsl:when test="Qualifier = 'Q1'">
              <Value1>
                <xsl:value-of select="Value/text()" />
              </Value1>
            </xsl:when>
            <xsl:when test="Qualifier = 'Q3'">
              <Value2>
                <xsl:value-of select="Value/text()" />
              </Value2>
            </xsl:when>
          </xsl:choose>
        </Header>
      </xsl:for-each>
    </ns0:Root>
  </xsl:template>
</xsl:stylesheet>

The XSLT i have written is not giving the desired output. I need to use xslt 1.0 only.The header in the output repeats only once

1 Answer 1

0

Try the following XSLT-1.0 code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"  version="1.0" xmlns:s0="http://SchemaIN" xmlns:ns0="http://SchemaOUT"  exclude-result-prefixes="s0 msxsl">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />

  <xsl:template match="/s0:TestDTM">
    <ns0:Root>
      <Header>
        <xsl:apply-templates select="Detail|ID|Ref" />
      </Header>
    </ns0:Root>
  </xsl:template>

  <xsl:template match="Detail">
    <xsl:element name="{concat('Value',substring(Qualifier,2,1))}">
      <xsl:value-of select="Value/text()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*|node()" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Output is:

<ns0:Root xmlns:ns0="http://SchemaOUT">
    <Header>
        <ID>1234</ID>
        <Ref>A1</Ref>
        <Value1>Name</Value1>
        <Value2>Address</Value2>
        <Value3>AddressType</Value3>
    </Header>
</ns0:Root>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the solution this works , this logic is one part of the xml and your answer works for it. I have updated the input XML and desired output. Does the same xslt also works for it ?
I added a template for handling other nodes. It removes the namespace. Without the namespace the solution could have been simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.