Re: [xsl] change element to sibling's child node

Subject: Re: [xsl] change element to sibling's child node
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Tue, 22 Aug 2006 15:25:33 +0530
Please try the following stylesheet:

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

<xsl:output method="xml" encoding="UTF-8" indent="yes" />

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

<xsl:template match="stream_loop">
 <stream_loop>
   <xsl:call-template name="loop">
     <xsl:with-param name="n0" select="../number_of_streams" />
     <xsl:with-param name="n" select="../number_of_streams" />
   </xsl:call-template>
 </stream_loop>
</xsl:template>

<xsl:template match="CGMS_indicator" />

<xsl:template match="c_definition">
 <xsl:copy>
   <xsl:copy-of select="service_type" />
   <xsl:copy-of select="../CGMS_indicator" />
   <PCR_PID_indicator>4112</PCR_PID_indicator>
   <xsl:apply-templates select="node()[not(self::service_type)]" />
 </xsl:copy>
</xsl:template>

<xsl:template name="loop">
 <xsl:param name="n0" />
 <xsl:param name="n" />

 <xsl:if test="$n &gt; 0">
   <stream>
     <xsl:copy-of select="stream_definition/stream_type" />
     <PID><xsl:value-of select="4112 + 2*($n0 - $n)" /></PID>
     <ECM><xsl:value-of select="4112 + 2*($n0 - $n) + 1" /></ECM>
     <xsl:copy-of select="stream_definition/descriptors_loop_length" />
     <xsl:copy-of select="stream_definition/descriptor_loop" />
   </stream>
   <xsl:call-template name="loop">
     <xsl:with-param name="n0" select="$n0" />
     <xsl:with-param name="n" select="$n - 1" />
   </xsl:call-template>
 </xsl:if>
</xsl:template>

</xsl:stylesheet>


On 8/22/06, Lin, Jessica <jlin@xxxxxxxxxxx> wrote:

I need to do xml to xml transform. Here is my problem. Could you please give me the help?



Original XML file:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint>
       <co>
               <co_body>
                       <source_id>100</source_id>

                       <CGMS_indicator>0</CGMS_indicator>
                       <c_PIP_indicator>0</c_PIP_indicator>
                       <c_definition>
                               <service_type>100</service_type>
                               <HTTP_indicator>0</HTTP_indicator>
                               <number_of_streams>2</number_of_streams>
                               <root_ID>100</root_ID>
                               <stream_loop>
                                       <stream_definition>

<stream_type>2</stream_type>
                                               <SCID>100</SCID>

<descriptors_loop_length>compute</descriptors_loop_length>
                                               <descriptor_loop>

<about_descriptor>some description</about_descriptor>

<additional_descriptor>other description</additional_descriptor>
                                               </descriptor_loop>
                                       </stream_definition>
                               </stream_loop>
                       </c_definition>
               </co_body>
       </co>
</blueprint>




Need to do following transforms:


move CGMS_indicator and its text value under c_definition
in c_definition:
       add new element called PCR_PID_indicator, the text value equals
to the PID value of first stream in stream loop
       calculate stream number which start from root_ID and end with
root_ID+ number_of_streams, the gap is 1
       copy descriptors_loop_length and descriptor_loop from
c_definition\stream_loop\stream_definition to c_definition
in stream_loop
       rename stream_definition to stream
       recursive create stream element number_of_streams times
in each stream i:
       rename SCID to PID, the text value equals 4112+2*i
       add new element called ECM, the text value equals PID+1


the result XML file:


<?xml version="1.0" encoding="UTF-8"?>
<blueprint>
       <co>
               <co_body>
                       <source_id>100</source_id>

                       <c_PIP_indicator>0</c_PIP_indicator>
                       <c_definition>
                               <service_type>100</service_type>
                               <CGMS_indicator>0</CGMS_indicator>

<PCR_PID_indicator>4112</PCR_PID_indicator>
                               <HTTP_indicator>0</HTTP_indicator>
                               <number_of_streams>2</number_of_streams>
                               <root_ID>100</root_ID>
                               <stream_loop>
                                       <stream>

<stream_type>2</stream_type>
                                               <PID>4112</PID>
                                               <ECM>4113</ECM>

<descriptors_loop_length>compute</descriptors_loop_length>
                                               <descriptor_loop>

<about_descriptor>some description</about_descriptor>

<additional_descriptor>other description</additional_descriptor>
                                               </descriptor_loop>
                                       </stream>
                                       <stream>

<stream_type>2</stream_type>
                                               <PID>4114</PID>
                                               <ECM>4115</ECM>

<descriptors_loop_length>compute</descriptors_loop_length>
                                               <descriptor_loop>

<about_descriptor>some description</about_descriptor>

<additional_descriptor>other description</additional_descriptor>
                                               </descriptor_loop>
                                       </stream>

                               </stream_loop>
                       </c_definition>

<descriptors_loop_length>compute</descriptors_loop_length>
                       <descriptor_loop>
                               <about_descriptor>some
description</about_descriptor>
                               <additional_descriptor>other
description</additional_descriptor>
                       </descriptor_loop>
               </co_body>
       </co>
</blueprint>




Thanks,


Jesscia


--
Regards,
Mukul Gandhi

http://gandhimukul.tripod.com

Current Thread