 |
| 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
|
|
|
|

May 23rd, 2005, 07:40 AM
|
|
Authorized User
|
|
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help: Select every node except these nodes
I have a XML-structure like this:
<DOCUMENT>
<USER>Name</USER>
<HTMLDATA>
<p>
<b>Hi there!</b>
<OBJECT>
<TYPE>Image</TYPE>
<SOURCE>http://www.hi.com/image.jpg</SOURCE>
</OBJECT>
<i>This is transformed by XSL</i>
</p>
</HTMLDATA>
</DOCUMENT>
Tags with CAPITAL LETTERS are my XML data tags, and tags with small letters are my HTML output data:
I want to copy everything between the HTMLDATA tags and insert it into my HTML-page.
Like this:
<html>
<head>
<title>XSL Transforms</title>
</head>
<body>
<xsl:copy-of select="//HTMLDATA">
</body>
</html>
But I do not want to copy the <OBJECT>-tag because it belongs to my XML data and should be informative for the transform.
I want the output to be:
<html>
<head>
<title>XSL Transforms</title>
</head>
<body>
<p>
<b>Hi there!</b>
<img src="http://www.hi.com/image.jpg" />
<i>This is transformed by XSL</i>
</p>
</body>
</html>
How do I write my <xsl:copy-of> tag to get this output?
Maybe like this:
<xsl:copy-of select="//HTMLDATA/*[but not the OBJECT tag, the OBJECT tag should be transformed with some XSL code futher down]">
|
|

May 23rd, 2005, 08:26 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
xsl:copy-of will only do an exact copy. To copy most elements but not all, write an identity template
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
and then override it:
<xsl:template match="OBJECT"/>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 23rd, 2005, 08:32 AM
|
|
Authorized User
|
|
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How do I navigate to the position of HTMLDATA, before I do the * selection?
EDIT: I mean, don't I have to do "cd /DOCUMENT/HTMLDATA" before I do "ls *" analogously?
|
|

May 23rd, 2005, 08:49 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
How you get to somewhere depends entirely on where you are starting from.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 24th, 2005, 03:51 AM
|
|
Authorized User
|
|
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dear Michael,
I have done the implementation just as you suggested, but I get a strange result.
I first get a copy of the data between the HTMLDATA tags. Then, I get the data without the tags.
Like this:
<p>
<b>Hi there!</b>
<OBJECT>
<TYPE>Image</TYPE>
<SOURCE>http://www.hi.com/image.jpg</SOURCE>
</OBJECT>
<i>This is transformed by XSL</i>
</p>
Hi thereImageThis is transformed by XSL
The OBJECT-tag is overridden, however.
|
|

May 24th, 2005, 04:46 AM
|
|
Authorized User
|
|
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Instead, I am now using:
<xsl:copy-of select="/DOCUMENT/HTMLDATA/*[name() != 'OBJECT']"/>
That doesn't work, but if I replace with this:
<xsl:copy-of select="/DOCUMENT/HTMLDATA/*[name() != 'i']"/>
The data between the <i>-tags disappears.
Why is that so?
|
|

May 24th, 2005, 05:49 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You haven't understood the relationship between apply-templates and match patterns. Match patterns are conditions that a node must satisfy, they don't say how to reach the node. You say how to reach the node in the select attribute of xsl:apply-templates. Match patterns never need a "//" at the start for that reason.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 24th, 2005, 06:44 AM
|
|
Authorized User
|
|
Join Date: Mar 2005
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't get anywhere with this:
<xsl:template match="/DOCUMENT">
<html>
<body>
<xsl:apply-templates select="HTMLDATA"/>
</body>
</html>
</xsl:template>
<xsl:template match="HTMLDATA">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="OBJECT"/>
I feel like there's a missing link somewhere in my code. I have your book XSLT - Programmer's Reference, where do I find info about this in particular?
|
|

May 25th, 2005, 08:07 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/DOCUMENT">
<html>
<body>
<xsl:apply-templates select="HTMLDATA" />
</body>
</html>
</xsl:template>
<xsl:template match="HTMLDATA">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="OBJECT[TYPE='Image']">
<img src="{SOURCE}" />
</xsl:template>
</xsl:stylesheet>
Regards
Bryan
|
|
 |