0
votes

I want to get below output from my input xml using xslt 1.0. Can you please suggest a way to do that.

    Input xml:
    <result>
      <item0>
        <Name>Customer1</Name>
        <location>1360190</location>
      </item0>
     <item1>
       <Name>Customer2</Name>
      <location>1360190</location>
    </item1>
  </result>

     Output xml:
      <result>
        <item>
          <Name>Customer1</Name>
          <location>1360190</location>
        </item>
        <item>
          <Name>Customer2</Name>
          <location>1360190</location>
        </item>
      </result>
1
Use the Identity Transform template, example, for copying all the nodes as-is. And add another template that matches /result/* i.e., the children elements of result and transform them as required. - Lingamurthy CS

1 Answers

0
votes

As @LingamurthyCS mentions, consider the Identity Transform and template to rename <item> nodes with various XPath match references as shown below:

With result reference and apply-templates on child elements:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

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

  <xsl:template match="result/*">
    <item>
      <xsl:apply-templates select="*"/>
    </item>
  </xsl:template>               

</xsl:transform>

Without result reference and copy-of on child elements:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

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

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

</xsl:transform>

With item reference:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

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

  <xsl:template match="*[contains(name(), 'item')]">
    <item>
      <xsl:apply-templates />
    </item>
  </xsl:template>               

</xsl:transform>