Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2005, 10:17 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:for-each-group Questions

I have a list of FAQ's in an XML doc (see example below), and the tags within the "answer" node use <p>, <ol> and other standard tags. I want to use an XSLT stylesheet to convert these child nodes to liternal HTML tags after an XHTML transformation.

So here's an example XML doc:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<faq>
    <question>This is where the question would be placed?</question>
        <answer>
            <p>This is where the answer would be placed:</p>
                <ol>
                    <li>This would be the first point.</li>
                    <li>This would be the second point.</li>
                    <li>This would be the third point.</li>
            <p>This is where a sub sentence would be placed.</p>
        </answer>
</faq>
...and this is the XSLT stylesheet that I created to display these tags literally in the transformation to XHTML:
Code:
<xsl:template match="answer">

    <xsl:for-each-group select="p" group-by="p">
        <p><xsl:value-of select="current-grouping-key()"/></p>
    </xsl:for-each-group>


    <xsl:for-each-group select="em" group-by="p">
        <em><xsl:value-of select="current-grouping-key()"/></em>
    </xsl:for-each-group>


    <xsl:for-each-group select="u" group-by="p">
        [u]<xsl:value-of select="current-grouping-key()"/></u>
    </xsl:for-each-group>


    <xsl:for-each-group select="ol" group-by="p">
        <ol><xsl:value-of select="current-grouping-key()"/></ol>
    </xsl:for-each-group>


    <xsl:for-each-group select="ul" group-by="p">
        [list]<xsl:value-of select="current-grouping-key()"/></ul>
    </xsl:for-each-group>


    <xsl:for-each-group select="li" group-by="p">
        <li><xsl:value-of select="current-grouping-key()"/></li>
    </xsl:for-each-group>

</xsl:template>
But I keep receiving this error message:
System.Runtime.InteropServices.COMException: Keyword xsl:for-each-group may not be used here.

So I reviewed several online resources on XSLT Grouping, including http://www.xml.com/lpt/a/2003/11/05/tr.html and http://www.w3.org/TR/xslt20/#grouping-examples. I'm following the rules that were set in the examples included in these examples, but I'm still getting that message.

I even copied & pasted this example directly from the xml.com site:
XML:
Code:
<html><body>
<h1>Loomings</h1>
<p>par 1</p>
<p>par 2</p>
<p>par 3</p>
<h1>The Whiteness of the Whale</h1>
<p>par 4</p>
<p>par 5</p>
<p>par 6</p>
</body>
</html>
XSLT:
Code:
<xsl:template match="body">
  <body>
    <xsl:for-each-group select="*" group-starting-with="h1">
      <chapter>
      <xsl:for-each select="current-group()">
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:for-each>
      </chapter>
    </xsl:for-each-group>
  </body>
</xsl:template>
Resulting XHTML:
[code]<html>
   <body>
      <chapter>
         <h1>Loomings</h1>
         <p>par 1</p>
         <p>par 2</p>
         <p>par 3</p>
      </chapter>
      <chapter>
         <h1>The Whiteness of the Whale</h1>
         <p>par 4</p>
         <p>par 5</p>
         <p>par 6</p>
      </chapter>
   </body>
</html>[code]
... but still received the error. So if anyone could let me know what I'm missing, it would be greatly appreciated. Thanks.

KWilliams
 
Old July 22nd, 2005, 01:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

xsl:for-each-group is an XSLT 2.0 instruction, and you will get an error if you try to use it with an XSLT 1.0 processor.

For an XSLT 2.0 processor, try Saxon 8 from http://saxon.sf.net/

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 22nd, 2005, 03:16 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very good. Thanks for another quick and thorough reply.

KWilliams
 
Old July 25th, 2005, 02:20 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Michael,

Sorry about this, but I had to ask a few more questions about XSLT 1.0 vs. 2.0:

1) Am I correct in thinking that most browsers support 1.0 but not 2.0 yet?

2) If not, what is the current status of browser compatability with 1.0 & 2.0?

If so:
3) Wouldn't it be more wise for me to develop my site using 1.0 for now until 2.0 is accepted by more if not all browsers?
4) What methods exist with 1.0 that display tags within an XML tag as literal HTML?
Example:
<root>
 <element>
  <child>
   <h1>TEST</h1>
   <p>This is a test</p>
   <p>This is only a test</p>
  </child>
 </element>
</root>

transforms to this using an XSLT stylesheet:

<html>
<body>
 <h1>TEST</h1>
  <p>This is a test</p>
  <p>This is only a test</p>
</body>
</html>

Thanks for your input Michael.

KWilliams
 
Old July 25th, 2005, 04:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, there is no browser support for XSLT 2.0 yet, and it will be some time before there is. If you want to do the transformations client-side, you have to use XSLT 1.0.

But many people are using XSLT on the server, and there you do have a choice.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 25th, 2005, 04:16 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Concerning your question 4:

<xsl:template match="child">
  <xsl:copy-of select="*"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 25th, 2005, 04:20 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, now I understand. Since I'm doing the transformation server-side and not client-side, I can download XSLT 2.0 through the link that you provided directly on the server. Am I correct?

Also, concerning your answer to question #4, thank you for that information. I'll make sure to give it a try asap. Thanks again for the quick and thorough responses.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xsl: strip-space elements; and 'group-adjacent' ROCXY XSLT 6 July 15th, 2010 08:59 AM
XSL group/sort by date problem athos XSLT 4 October 7th, 2008 09:35 AM
How to group xsl elements in xslt.10 suji XSLT 1 February 29th, 2008 03:44 AM
Restart new group number in Group Footer sukarso Crystal Reports 2 October 13th, 2006 12:11 PM
Clarification require on use - xsl:for-each-group ROCXY XSLT 1 January 2nd, 2006 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.