XSLT Transformation for nested xml

I am trying to convert xml into soap request using XSLT transformation policy

XML:-

<Root>
  <reservationItem>
    <hotelName>
      <date>2022-03-09</date>
      <bookingDetails>
        <baseAmount>20000</baseAmount>
        <discAmount>10000</discAmount>
        <season>Prime</season>
      </bookingDetails>
    </hotelName>
    <hotelName>
      <date>2022-03-10</date>
      <bookingDetails>
        <baseAmount>20000</baseAmount>
        <discAmount>10000</discAmount>
        <season>Prime</season>
      </bookingDetails>
    </hotelName>
  </reservationItem>
</Root>

After applying below XSLT

<xsl:if test="Root/reservationItem/hotelName">
     <xsl:for-each select="Root/reservationItem/hotelName">
         <v1:hotelName>
            <xsl:value-of select="." />
         </v1:hotelName>
     </xsl:for-each>
</xsl:if>



getting below result (some ID instead of xml structure)
<v1:hotelName>3151RTN4nullUSD19.0</v1:hotelName>
<v1:hotelName>2022-03-092000010000167750585Prime</v1:hotelName>

@anil

0 1 401
1 REPLY 1

You're getting something you don't want from that XSL Transform.

But the XSL Transform is working correctly. The transform is doing what you told it to do, with that XSL file. For example, with this snippet:

 

 

   <v1:hotelName>
      <xsl:value-of select="." />
   </v1:hotelName>

 

 

...you are telling the XSL processor to emit all of the text values of any child of the hotelName element. Which is why in the output you see 092000010000167750585Prime . That is the concatenation of all of the text values of every element in the tree beneath hotelName . String concatenation is probably not what you want, but .... that stylesheet is working "as directed". So I think you need to change your XSL file.

You showed the input XML, and the XSL, and the observed output. but you didn't specify the DESIRED output. What do you WANT it to be?

Maybe you want XSL that is closer to this?

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="/Root">
    <root xmlns:v1="foo">
     <xsl:for-each select="reservationItem/hotelName">
       <v1:hotelName>
        <xsl:apply-templates select="child::node()" />
       </v1:hotelName>
     </xsl:for-each>
    </root>
  </xsl:template>

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

</xsl:stylesheet>

When I run your data through that XSL, I get this: 

<root xmlns:v1="foo">
    <v1:hotelName>
      <date>2022-03-09</date>
      <bookingDetails>
        <baseAmount>20000</baseAmount>
        <discAmount>10000</discAmount>
        <season>Prime</season>
      </bookingDetails>
    </v1:hotelName>
    <v1:hotelName>
      <date>2022-03-10</date>
      <bookingDetails>
        <baseAmount>20000</baseAmount>
        <discAmount>10000</discAmount>
        <season>Prime</season>
      </bookingDetails>
    </v1:hotelName>
</root>

PS: you can use online XSL test tools like this one, to try out your XSL.