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 January 7th, 2014, 01:32 PM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Processing mixed content nodes

Hello again,

I am seeing lots of examples on how to process well-formed, completely structured XML documents, but none about processing nodes that contain both text and child nodes. What I need to do is process the child nodes while keeping the surrounding text and the location of the child nodes intact.

Here is a little part of my document:

<p>The <objectref>Book</objectref> object represents a book file. Its components may be <objectref>Doc</objectref> objects or nested <objectref>Book</objectref> objects, all of which are organized in a linked list of <objectref>BookComponent</objectref> objects.</p>

This paragraph is itself part of the output of a larger XSLT, to which I need to add a template to process the <objectref> element without removing any of the other stuff. They should automagically turn into hyperlinks (unless if that hyperlink would point to the current document).

I am probably misunderstanding something in the way templates are applied: when that happens and how to determine which templates are called at what point. A concrete example to handle the above could help. All the examples that merely handle with XML that has no mixed content whatsoever do not.

Thanks

Jang
 
Old January 7th, 2014, 01:55 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Whether it is mixed content or not does not matter, the basic approach is always starting with
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
then you add templates for those tasks you want to perform e.g.
Code:
<xsl:template match="objectref">
  <a href="{.}">
    <xsl:apply-templates/>
  </a>
</xsl:template>
You have not really shown what you want to put in the href attribute of the hyperlink to be created but as far as transforming some elements while copying the rest unchanged the approach is as given above.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 7th, 2014, 02:08 PM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I did find something in the meantime and that seems to work. It does show a slight difference to your code snippet:

<xsl:template match="p">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

This would of course only work in the <p> that activates the other templates. And I understand now that text nodes and child nodes are all processed by the copy mechanism, as long as I add the select="@*". I guess that is shorthand for your select string?

Is the above code (which I found in the O'Reilly XSLT book) doing exactly the same as your shorter code?

As for the href: I will be using the objectref's name as a target with ".html" attached to it. That is pretty straightforward and already works. Now I need to figure out how to make the exception work when I am already on the page that would appear in the href, as I do not want it to appear as a link in that case. More work. More to learn, every day.

Thanks

Jang
 
Old January 7th, 2014, 02:15 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're on a Wrox forum, and Wrox publishes lots of good XML books, so one might reasonably ask which ones you've been reading and which parts you don't understand.

In my book, XSLT 2.0 Programmer's Reference, Chapter 18 is a detailed case study of a stylesheet used for rendering narrative XML into HTML, which is essentially the kind of problem you are tackling. I would recommend studying it, it will give you lots of ideas.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 7th, 2014, 02:30 PM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello Michael,

Thanks for the pointer. I do have your book, studied some of the earlier chapters in detail and used the rest of it for reference while trying to make things work. I did not get to chapter 18 yet, but I will certainly read it asap. I will probably find ways to do my stuff more elegantly or more efficiently.

Kind regards

Jang





Similar Threads
Thread Thread Starter Forum Replies Last Post
checking mixed content model riteshdubey5 XSLT 1 September 28th, 2012 10:18 AM
Textual content in mixed content model flockofcode BOOK: Beginning XML, 4th Ed ISBN: 978-0-470-11487-2 2 May 11th, 2011 03:54 PM
Mixed content in DTDs GrantRobertson BOOK: Beginning XML, 4th Ed ISBN: 978-0-470-11487-2 0 November 2nd, 2009 12:40 AM
Processing specific Nodes Krippers XSLT 3 November 6th, 2008 05:38 AM
mixed content parsing joeri XSLT 2 October 7th, 2007 04:54 PM





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