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 December 18th, 2003, 06:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default XSLT Template error again

Hello,

I thought my problem was fixed, but it is not. My xml is in the format of:

<Documents>
  <Section Title="A" Sidebar="false">
    <Document> <- maxOccurs="unbounded", contains child nodes
    ...
  </Section>
  <Section Title="B" Sidebar="false">
    ...
  </Section>
  <Section Title="C" Sidebar="true">
    ...
  </Section>

I want the sidebar to be separate from the regular content. My stylesheet looks like:

<xslt:template match="Documents">
  <table>
    <tr>
      <td>
         <xslt:apply-templates select="Section[@Sidebar='false']"/>
         <!--
         I also tried this but it didn't work:
         <xslt:for-each select="Section[@Sidebar='false']">
            <xslt:apply-templates select="Section"/>
         </xslt:for-each>
         -->
      </td>
      <td style="border: 1px solid black">
         <xslt:apply-templates select="Section[@Sidebar='true']"/>
         <!--
         I also tried this but it didn't work:
         <xslt:for-each select="Section[@Sidebar='true']">
            <xslt:apply-templates select="Section" mode="sidebar"/>
         </xslt:for-each>
         -->
      </td>
    </tr>
</xslt:template>

For the first option that is uncommented, I matched them as:
<xslt:template match="Section[@Sidebar='false']">
  ....
</xslt:template>
<xslt:template match="Section[@Sidebar='true']">
  ....
</xslt:template>

For the uncommented code attempt, I matched them as:
<xslt:template match="Section">
  ....
</xslt:template>
<xslt:template match="Section" mode="sidebar">
  ....
</xslt:template>

The first code attempt worked, but everything was Sidebar='false', so it didn't work. My last attempt didn't work also, was even worse.

Any ideas how to do this?

Thanks,

<Brian Mains/>
__________________
Brian
 
Old December 18th, 2003, 09:07 PM
Authorized User
 
Join Date: Oct 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,

I've never seen "<xslt:..." used in stylesheets -- this is a typo? Should be "<xsl:...". Otherwise, one issue I can see immediately is this code:

         <xsl:for-each select="Section[@Sidebar='false']">
            <xsl:apply-templates select="Section"/>
         </xsl:for-each>

won't work because this is attempting to apply templates to Section element children of Section elements where @Sidebar is false; i.e it will only match:

<Section Sidebar="false">
  <Section>matches these elements</Section>
</Section>

However, looking at your stylesheet I would ask what your ultimate objective is regarding the table to be output. As it stands, it looks like you want one table with one row (<tr>) per Documents element, with the first "cell" (<td>) containing everything where @Sidebar is "false", and 2nd cell containing everything where @Sidebar is "true". Is this correct? If so, try:

<xsl:template match="Documents">
  <table>
    <tr>
      <td>
         <xsl:apply-templates select="Section[@Sidebar='false']"/>
      </td>
      <td style="border: 1px solid black">
         <xsl:apply-templates select="Section[@Sidebar='true']" mode="sidebar"/>
      </td>
    </tr>
  </table>
</xsl:template>

<xsl:template match="Section">
  code for which @Sidebar is "false"
</xsl:template>
<xsl:template match="Section" mode="sidebar">
  code for which @Sidebar is "true"
</xsl:template>

untested...

...sam

 
Old December 19th, 2003, 09:38 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Not sure what you want but I made up a stylesheet for use on the following xml. If your output for true and false sections is the same you might not need two templates to match, just two to slect. That's why I coloured the output.
Code:
<Documents>
  <Section Title="A" Sidebar="false">
    <Document><data>Joe</data></Document>
    <Document><data>Gins</data></Document>
    <Document><data>Sephi</data></Document>
  </Section>
  <Section Title="B" Sidebar="false">
    <Document><data>Xavier</data></Document>
    <Document><data>Peter</data></Document>
    <Document><data>Stephen</data></Document>
  </Section>
  <Section Title="C" Sidebar="true">
    <Document><data>William</data></Document>
    <Document><data>Sheila</data></Document>
    <Document><data>Smokie</data></Document>
  </Section>
</Documents>
The xslt is
Code:
<woodenShoe:stylesheet version="1.0" xmlns:woodenShoe="http://www.w3.org/1999/XSL/Transform">
  <woodenShoe:output method="html"/>
  <woodenShoe:template match="/">
    <html>
      <head><title>True versus False</title></head>
      <body>
        <woodenShoe:apply-templates select="Documents"/>  
      </body>
    </html>    
  </woodenShoe:template>
  <woodenShoe:template match="Documents">
    <table border="1">
      <tbody>
        <tr>
          <td>    
            <woodenShoe:apply-templates select="Section[@Sidebar = 'false']"/>
          </td>
          <td>    
            <woodenShoe:apply-templates select="Section[@Sidebar = 'true']"/>
          </td>
        </tr>    
      </tbody>
     </table>
  </woodenShoe:template>
  <woodenShoe:template match="Section[@Sidebar = 'false']">
    <div style="color:#dd0000"><woodenShoe:value-of select="@Title"/>:</div>
    <woodenShoe:apply-templates select="Document"/>    
  </woodenShoe:template>
  <woodenShoe:template match="Section[@Sidebar = 'true']">
    <div style="color:#00dd00"><woodenShoe:value-of select="@Title"/>:</div>
    <woodenShoe:apply-templates select="Document"/>  
  </woodenShoe:template>
  <woodenShoe:template match="Document">
    #160;#160;<woodenShoe:value-of select="data/text()"/><br/>
  </woodenShoe:template>
</woodenShoe:stylesheet>
Personally I would have thought you wanted one column for true, one for false and each item in a fresh row but that's not difficult to manage either.

My apologies to shbyland for the woodenShoe bit...

Joe (MVP - xml)
 
Old December 19th, 2003, 10:55 AM
Authorized User
 
Join Date: Oct 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No apologies needed!!

so the declaration line (<woodenShoe:stylesheet version="1.0" xmlns:woodenShoe="http://www.w3.org/1999/XSL/Transform">) defines all (learn something new every day) :)

...sam


 
Old December 19th, 2003, 11:17 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Yes, two other things may, or may not, be of interest. You can use transform instead of styleheet, obviously the w3c committee couldn't agree on what to call it.
Secondly you can miss off the prefix altogether but then you would have to use a prefix for any non-xslt elements which would only suit certain output styles..
Code:
<transform version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"                           xmlns:myNS="http://rightway.co.uk/namespaces/general/examples">
  <template match="/">
    <myNS:root><myNS:data><value-of select="'Hello World.'"/></myNS:data></myNS:root>    
  </template>
</transform>
Joe (MVP - xml)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stumped on XSLT template hayedid XSLT 8 December 19th, 2007 02:14 PM
XSLT: ONE template to transform all the elements Behl_Neha XSLT 8 December 15th, 2007 07:31 PM
XSLT Template Issue saravanan.k XSLT 2 March 14th, 2007 06:41 AM
Problem in passing parameter to xslt template uttamgarg XSLT 0 April 20th, 2006 09:53 AM
XSLT - First Time Matching a Template highlife XSLT 4 July 25th, 2003 07:50 AM





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