Subject: Replacing elements name while grouping.
Posted By: ROCXY Post Date: 1/6/2006 4:40:50 AM
Hi All,

I have an XML.

<artbody>
<sec>   
 <heading1>
        <verse>Kya, karein mere n;-)</verse>
        <verse>Kya, old angry man hoon ;-)</verse>
   </heading1>
</sec>    
<sec>
  <subsec>
        <verse>Kya, karein mere n;-)</verse>
        <verse>Kya, old angry man hoon ;-)</verse>
  </sebsec>
</sec>
</artbody>

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

<xsl:template match="verse">
  <xsl:if test="not(name(preceding-sibling::*[1]) = 'verse')">
    <quote>
    <xsl:copy-of select=".|following-sibling::verse"/>
    </quote>
  </xsl:if>
</xsl:template>

<xsl:template match="verse">
<xsl:copy>
<para><xsl:apply-templates/></para>
</xsl:copy>
</xsl:template>

Output XML.
-----------
<artbody>
   <sec>   
      <heading1>
        <verse>
            <para>Kya, karein mere n;-)</para>
         </verse>
        <verse>
            <para>Kya, old angry man hoon ;-)</para>
         </verse>
      </heading1>
   </sec>    
   <sec>
      <subsec>
        <verse>
            <para>Kya, karein mere n;-)</para>
         </verse>
        <verse>
            <para>Kya, old angry man hoon ;-)</para>
         </verse>
      </subsec>
   </sec>
</artbody>
---------------------------------------------------------------------

As you see while grouping 'verse', I would like to change child <verse> to <para> and this should happen after grouping all child 'verse' inside 'quote' like this....

....
<quote>
<para>Kya, karein mere n;-)</para>
<para>Kya, old angry man hoon ;-)</para>
</quote>
......

I would greatly appreciate any help provided.

Thanks & Regards,
- ROCXY


Reply By: mhkay Reply Date: 1/6/2006 8:18:55 AM
You have two template rules that match verse. The XSLT spec is a bit ambivalent on this: it says that it's an error, but the processor doesn't have to report the error, it can recover by choosing the last rule that matches.

What you are trying to achieve is to process the verse twice. The way to do that is with a multi-phase transformation (a mini pipeline). Capture the result of the first transformation in a variable

<xsl:variable name="temp">
  <xsl:apply-templates/>
</xsl:variable>

then apply templates to this, in a different mode:

<xsl:apply-templates select="$temp" mode="phase-2"/>

Only, in an XSLT 1.0 processor, you may have to call the vendor's xx:node-set() extension function to make this work:

<xsl:apply-templates select="xx:node-set($temp)" mode="phase-2"/>

where the namespace bound to xx depends on the XSLT processor you are using.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: ROCXY Reply Date: 1/6/2006 11:52:45 AM
Dear kay,

I was trying this syntax but - I dont know how to frame it exactly inside the XSL.

Thanks & Regards,
-ROCXY

Reply By: ROCXY Reply Date: 1/6/2006 12:11:54 PM
Hi Kay,

I used the following code its working fine.

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

<xsl:template match="verse">
  <xsl:if test="not(name(preceding-sibling::*[1]) = 'verse')">
    <quote>
    <xsl:apply-templates select=". | following-sibling::verse" mode="phase1"/>
    </quote>
  </xsl:if>
</xsl:template>

<xsl:variable name = "temp">
<xsl:apply-templates select="node() | @*" mode="phase1"/>
</xsl:variable>

<xsl:template match="verse" mode="phase1">
<para>
<xsl:apply-templates />
</para>
</xsl:template>

Thanks
-ROCXY


Go to topic 36501

Return to index page 488
Return to index page 487
Return to index page 486
Return to index page 485
Return to index page 484
Return to index page 483
Return to index page 482
Return to index page 481
Return to index page 480
Return to index page 479