xsl-list
[Top] [All Lists]

Re: [xsl] Issue with repetition of elements in the input XML

2007-11-27 09:43:24
I am glad that finally you are able to work out a solution.

I would also go with David's advice.

If you are able to use XSLT 2.0, I would strongly suggest, please use
XSLT 2.0. It's now a W3C Recommendation, and there are stable 2.0
implementations existing.

If there is no choice but to use XSLT 1.0, please let me know, and I
can try to convert the solution to 1.0.

On 11/27/07, lakshmi mrudula <mrudula_lakshmi(_at_)yahoo(_dot_)com> wrote:
Hi,

With one more if condition to below approach its
working fine.

THanks for your solution.

Here is the XSL.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

       <xsl:output method="xml" indent="yes"/>

       <xsl:variable name="controlInfo">
               <CompanyId/>
               <ReservationControlNumber/>
               <ReservationControlType/>
               <FirstDate/>
               <Time/>
       </xsl:variable>

       <xsl:template match="/root">
               <root>
                       <RCI>
                               <xsl:for-each-group
select="ReservationControlInformationSegment/*"
group-starting-with="CompanyId">

                                       <xsl:variable
name="aReservationControlInformation">RCI<xsl:number
value="position()" format="01"/></xsl:variable>
                                       <xsl:element
name="{$aReservationControlInformation}">
                                               <xsl:for-each 
select="$controlInfo/*">
                                                       <xsl:variable
name="aReservationControlInformationNew">
                                                               <xsl:value-of
select="$aReservationControlInformation"/>
                                                               <xsl:number 
value="position()" format="01"/>
                                                       </xsl:variable>
                                                       <xsl:if 
test="current-group()[local-name() =
local-name(current())]">
                                                       <xsl:element
name="{$aReservationControlInformationNew}">
                                                               <xsl:value-of
select="current-group()[local-name() =
local-name(current())]"/>
                                                       </xsl:element>
                                                       </xsl:if>
                                               </xsl:for-each>
                                       </xsl:element>
                               </xsl:for-each-group>
                       </RCI>
               </root>
       </xsl:template>
</xsl:stylesheet>


When we tried this XSL with StylusStudio its able to
convert the input XML. But with eclipse its not
working. This is because we are using XSLT 1.0 and
for-each-group is not defined for that. Can you please
provide us the solution in XSLT 1.0. What will be the
equivalent of for-each-group.

--- Mukul Gandhi <gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:

Please try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        version="2.0">

 <xsl:output method="xml" indent="yes" />

 <xsl:variable name="controlInfo">
   <CompanyId/>
   <ReservationControlNumber/>
   <ReservationControlType/>
   <FirstDate/>
   <Time/>
 </xsl:variable>

 <xsl:template match="/root">
   <root>
     <RCI>
       <xsl:for-each-group
select="ReservationControlInformationSegment/*"
group-starting-with="CompanyId">

         <xsl:variable
name="aReservationControlInformation">RCI<xsl:number
                       value="position()"
format="01"/>
         </xsl:variable>
         <xsl:element
name="{$aReservationControlInformation}">
           <xsl:for-each select="$controlInfo/*">
              <xsl:variable
name="aReservationControlInformationNew">
                 <xsl:value-of
select="$aReservationControlInformation"
/><xsl:number
                           value="position()"
format="01"/>
              </xsl:variable>
              <xsl:element
name="{$aReservationControlInformationNew}">
                <xsl:value-of
select="current-group()[local-name() =
local-name(current())]" />
              </xsl:element>
           </xsl:for-each>
         </xsl:element>
       </xsl:for-each-group>
     </RCI>
   </root>
 </xsl:template>

</xsl:stylesheet>

On Nov 23, 2007 10:57 AM, lakshmi mrudula
<mrudula_lakshmi(_at_)yahoo(_dot_)com> wrote:
Hi,

Thanks for your solution.
But with XSL logic below,
If input XML is as shown below:

<?xml version="1.0"?>
<root>
       <ReservationControlInformationSegment>
               <CompanyId>AAH</CompanyId>



<ReservationControlNumber>ABC12345</ReservationControlNumber>

<ReservationControlType>A</ReservationControlType>
               <FirstDate>11OCT2007</FirstDate>
               <Time>1230</Time>
               <CompanyId>AAA</CompanyId>


<ReservationControlType>B</ReservationControlType>
               <FirstDate>11OCT2007</FirstDate>
               <Time>1130</Time>
       </ReservationControlInformationSegment>
</root>

Then Expected output will be:

<?xml version='1.0' ?>
<root>
 <RCI>
   <RCI01>
     <RCI0101>AAH</RCI0101>
     <RCI0102>ABC12345</RCI0102>
     <RCI0103>A</RCI0103>
     <RCI0104>11OCT2007</RCI0104>
     <RCI0105>1230</RCI0105>
   </RCI01>
   <RCI02>
     <RCI0201>AAA</RCI0201>
     <RCI0202/>
     <RCI0203>B</RCI0203>
     <RCI0204>11OCT2007</RCI0204>
     <RCI0205>1130</RCI0205>
   </RCI02>
 </RCI>
</root>

But the actual output is coming with the given XSL
as
below:

<?xml version='1.0' ?>
<root>
 <RCI>
   <RCI01>
     <RCI0101>AAH</RCI0101>
     <RCI0102>ABC12345</RCI0102>
     <RCI0103>A</RCI0103>
     <RCI0104>11OCT2007</RCI0104>
     <RCI0105>1230</RCI0105>
   </RCI01>
   <RCI02>
     <RCI0201>AAA</RCI0201>
     <RCI0202>B</RCI0202>
     <RCI0203>11OCT2007</RCI0203>
     <RCI0204>1130</RCI0204>
   </RCI02>
 </RCI>
</root>


Our requirement for the output is:

 If any information is missing in the middle then
empty
 tag should be there.
 But if it is missing at the end, then empty tags
need
 not be there in the output.

 In input XML, elements will be in the same order.
None
 of the elements are mandatory.
 Any of the elements can be missed in the input
XML.

 For this requirement we need solution.




--- Mukul Gandhi <gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:

This problem is easily solved with the XSLT 2.0
grouping construct.
Please refer to the solution below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       version="2.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/root">
    <root>
      <RCI>
        <xsl:for-each-group
select="ReservationControlInformationSegment/*"
group-starting-with="CompanyId">
          <xsl:variable

name="aReservationControlInformation">RCI<xsl:number
                        value="position()"
format="01"/>
          </xsl:variable>
          <xsl:element
name="{$aReservationControlInformation}">
            <xsl:for-each
select="current-group()">
               <xsl:variable
name="aReservationControlInformationNew">
                  <xsl:value-of
select="$aReservationControlInformation"
/><xsl:number
                              value="position()"
format="01"/>
               </xsl:variable>
               <xsl:element
name="{$aReservationControlInformationNew}">
                 <xsl:value-of select="." />
               </xsl:element>
            </xsl:for-each>
          </xsl:element>
        </xsl:for-each-group>
      </RCI>
    </root>
  </xsl:template>

</xsl:stylesheet>


-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--