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 August 28th, 2008, 08:04 PM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Outputting values of a selection

I'm pretty new to XSLT and i'm trying to convert some xhtml into a nicer looking xml format. Basically i'm trying to convert some user comments into something that looks like
<comment>
   <from>me</from>
   <pic>url</pic>
   <message>hello world</message>
</comment>

and at the minute it looks like
Code:
<html>
<body>
...
<div id="header">...
<div id="content">
<table>
<tr>
<td valign="top" colspan="2">
 <img align="right" src="ProfilePic.jpg" />
 <a href="Profile.php?id=abc">Me</a>
 <br />
 hello world
</td>
</tr>
<tr>
 <td>
 <img align="right" src="ProfilePic.jpg">
 <a href="Profile.php?id=123">you</a>
 <br />
 hello world again
 </td>
</tr>
etc...
I was able to work out the XPath as i have experience using it but when it comes to converting all the information as much as i was able to get was
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html>
    <body>
    <h2>Hello World</h2>
    <b>Items</b>
    <table border="1">
  <xsl:for-each select="Xpath to the "<td>" for each comment">
    <tr>
     <xsl:value-of select="what goes here?">
    </tr>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
any help would be greatly appreciated :D

 
Old August 29th, 2008, 03:43 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

<xsl:template match="table">
  <html>
   <body>
    <h2>Hello World</h2>
    <b>Items</b>
    <table border="1">
  <xsl:apply-templates/>
    </table>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="td">
  <xsl:for-each select=".">
   <tr><td>
    <comment>
     <from><xsl:value-of select="a"/></from>
     <pic><xsl:value-of select="img/@src"/></pic>
     <message><xsl:value-of select="./text()"/></message>
    </comment></td>
   </tr>
  </xsl:for-each>
  </xsl:template>

 
Old August 29th, 2008, 06:48 AM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much but i now have a slight problem with my XPath

to exactly select the correct td elements i need this xpath
Code:
<xsl:template match="/*[local-name()='html']/*[local-name()='body']/*[local-name()='div' and @id='wrapper']/*[local-name()='div' and @id='content']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td' and @class='']/*[local-name()='br']/*parent::*">
I have tested this Xpath query in Stylus studio 2008 XML on my xhtml document and it returns the correct values but when putting it into the <xsl:template match> function it shows an error relating to the "/*parent::*"

Code:
[local-name()='br']/*parent...}:
    Unexpected token "<axis>" beyond end of pattern


Any ideas?

 
Old August 29th, 2008, 06:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You either need another / between then * and parent, or get rid of the *:


<xsl:template match="/*[local-name()='html']/*[local-name()='body']/*[local-name()='div' and @id='wrapper']/*[local-name()='div' and @id='content']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td' and @class='']/*[local-name()='br']/*/parent::*">


<xsl:template match="/*[local-name()='html']/*[local-name()='body']/*[local-name()='div' and @id='wrapper']/*[local-name()='div' and @id='content']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td' and @class='']/*[local-name()='br']/parent::*">

/- Sam Judson : Wrox Technical Editor -/
 
Old August 29th, 2008, 07:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

/*[....]/*parent::*

is not a valid XPath expression, I suspect you meant

/*[....]/parent::*

I tried this expression in Stylus Studio 2008 and it quite properly rejected it.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old August 30th, 2008, 06:40 PM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay thanks for all the help guys, but i'm still having trouble

This is my Xpath query that works in the query tester in Stylus Studio

Code:
/*[local-name()='html']/*[local-name()='body']/*[local-name()='div' and @id='wrapper']/*[local-name()='div' and @id='content']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td']/*[local-name()='table']/*[local-name()='tr']/*[local-name()='td' and @class='']/*[local-name()='br']/parent::*
and i will post my html file just so i can see what i am doing wrong here :( ( i want to be able to select the parts commented as "COMMENT 1" and "COMMENT 2" into the format describe in my first post.

http://www.megaupload.com/?d=Q0CVC9L2 <- html file uploaded

Thanks guys.

 
Old September 4th, 2008, 11:44 AM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the bump guys but i'm still having no luck

 
Old September 4th, 2008, 12:02 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you post the relevant HTML and the stylesheet you have so far in the forum? That link goes to some flash animation asking for a login.

--
  Martin Honnen
  Microsoft MVP - XML





Similar Threads
Thread Thread Starter Forum Replies Last Post
Outputting Array manih C++ Programming 2 June 14th, 2006 08:24 PM
Outputting table permissions crisan Access 1 May 25th, 2005 06:39 AM
Outputting an end of line y_simonson XSLT 1 February 5th, 2004 05:57 AM
page numbering while outputting agbeltekin Javascript 5 November 13th, 2003 12:03 PM





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