Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have an xslt
XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

  <xsl:output omit-xml-declaration="yes" indent="yes" />
  <xsl:strip-space elements="*"/>

  <!-- Default template : ignore unrecognized elements and text -->
  <xsl:template match="*|text()" />
  <xsl:key name="room-per-hotel" match="result" use="concat(AccommodationId,'-', RoomId)" />
  <!-- Match document root : add hotels element and process each children node of result -->
  <xsl:variable name="doc2" select="."/>
  <xsl:template match="/">
    <!--<xsl:apply-templates/>-->
    <!-- We assume that the XML documents are always going to follow the structure:
             result as the root node and xml_acc elements as its children -->

    <xsl:for-each select="$doc2/Success/result">
      <xsl:if test="position() = 1">

        <RuleMessage>
          <xsl:attribute name="HotelCode">
            <xsl:value-of select="AccommodationId"/>
          </xsl:attribute>

          <xsl:for-each select="$doc2/Success/result[count(. | key('room-per-hotel', concat(AccommodationIdAccommodationId,'-', RoomId))[1]) = 1]">
            <xsl:sort select="concat(AccommodationId,'-', RoomId)" />

            <xsl:variable name="doc1" select="."/>

          <xsl:element name="StatusApplication">
            <xsl:attribute name="RatePlanCode">
              <xsl:value-of select="RoomIdRatePlanIdCode"/>
            </xsl:attribute>
            <xsl:attribute name="InvTypeCode">
              <xsl:value-of select="RoomId"/>
            </xsl:attribute>
            <xsl:attribute name="RatePlanCategory">
              <xsl:value-of select='"BB"'/>
            </xsl:attribute>
          </xsl:element>

          <BookingRules>

            <xsl:for-each select="$doc1/StartDate/tokenize(.,',')">
              <xsl:variable name="pos" select="position()"/>
              <xsl:if test="$doc1/OpenClose/tokenize(.,',')[position()=$pos] !='' ">

              <xsl:element name="BookingRule">
                <xsl:attribute name="Start" >
                  <xsl:value-of select="."/>
                </xsl:attribute>
                <xsl:attribute name="End" >
                  <xsl:value-of select="."/>
                </xsl:attribute>
                <xsl:element name="RestrictionStatus">
                  <xsl:attribute name="Restriction">
                    <xsl:value-of select='"Master"'/>
                  </xsl:attribute>
                  <xsl:attribute name="Status" >
                    <xsl:value-of select="$doc1/OpenClose/tokenize(.,',')[position()=$pos]"/>
                  </xsl:attribute>

                </xsl:element>
              </xsl:element>

              </xsl:if>
            </xsl:for-each>



          </BookingRules>
          </xsl:for-each>
        </RuleMessage>

      </xsl:if>
    </xsl:for-each>

  </xsl:template>

</xsl:stylesheet>

which is producing the result as below
XML
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <OTA_HotelBookingRuleRS xmlns="http://vbookings.bookingwhizz.com/connectAPI.asmx">
         <RuleMessage HotelCode="5" xmlns="">
            <StatusApplication RatePlanCode="101739608_1" InvTypeCode="101739608" RatePlanCategory="BB"/>
            <BookingRules>
               <BookingRule Start="2015-07-15" End="2015-07-15">
                  <LengthsOfStay>
                     <LengthOfStay MinMaxMessageType="SetMinLOS" TimeUnit="Day" Time="1"/>
                  </LengthsOfStay>
               </BookingRule>
            </BookingRules>
            <StatusApplication RatePlanCode="101739608_2" InvTypeCode="101739608" RatePlanCategory="BB"/>
            <BookingRules>
               <BookingRule Start="2015-07-15" End="2015-07-15">
                  <LengthsOfStay>
                     <LengthOfStay MinMaxMessageType="SetMinLOS" TimeUnit="Day" Time="2"/>
                  </LengthsOfStay>
               </BookingRule>
            </BookingRules>
           </RuleMessage>
      </OTA_HotelBookingRuleRS>
   </soap:Body>
</soap:Envelope>

but i need the result as below
XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <OTA_HotelBookingRuleRS xmlns="http://vbookings.bookingwhizz.com/connectAPI.asmx">

      <RuleMessage HotelCode="5" xmlns="">
        <StatusApplication RatePlanCode="101739608_1" InvTypeCode="101739608" RatePlanCategory="BB" />
        <BookingRules>
          <BookingRule Start="2015-07-14" End="2015-07-14">
            <RestrictionStatus Restriction="Master" Status="Close" />
          </BookingRule>
        </BookingRules>
      </RuleMessage>
      <RuleMessage HotelCode="5" xmlns="">
        <StatusApplication RatePlanCode="101739608_2" InvTypeCode="101739608" RatePlanCategory="BB" />
        <BookingRules>
          <BookingRule Start="2015-07-14" End="2015-07-14">
            <RestrictionStatus Restriction="Master" Status="Close" />
          </BookingRule>
        </BookingRules>
      </RuleMessage>
      ....

    </OTA_HotelBookingRuleRS>
  </soap:Body>
</soap:Envelope>
Posted
Updated 15-Jul-15 22:58pm
v2
Comments
Andy Lanng 16-Jul-15 4:58am    
did you have a question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900