[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Processing inner elements

Subject: Re: Processing inner elements
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Fri, 29 Dec 2006 22:35:18 +0100
getting inner elements with xpath
Guy wrote:
This must be a trivial question but I cannot find any reference to the
standard way of processing this. Here is a fragment of XML:

<para>This is a sentence<break/>
	<image src=http://site.com/image/image.jpg/><break/>
	And a second sentence.
</para>

Assuming that you are at the <para> node, how does one typically transform
that into the obvious HTML that is required?

In pre-babelish time, a lot was "obvious"; back then, things were sooo easy. However, those times have long gone, and little is obvious these days (unfortunately), and certainly not transforming X to Y ;) If I misunderstood you (see below), can you add some more info and paste what you got so far and what the input/output should look like?


But let me give it a try nevertheless. I assume that the obvious thing to do is to match "para" nodes to "p" tags, "image" nodes to "img" tags and "break" nodes to "br" tags. This is trivial in XSLT and the easiest thing I can think of is a simple identity template. Which is "obvious" xslt way when doing XML to XML (or html) transformation (you request for a "standard" way of doing things, I'm not sure such a thing exists, but if so, this may be it ;) . Search the FAQ for more info (I also like to recommend chapter 8 of XSLT Cookbook 2nd ed., O'Reilly, which goes into this in great and clear detail)

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- will copy everything that does not need translation
including attribute and text nodes -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() |@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="image">
<img>
<!-- apply only attributes, image has no children -->
<xsl:apply-templates select="@*" />
</img>
</xsl:template>


<xsl:template match="para">
<p>
<xsl:apply-templates select="node() |@*" />
</p>
</xsl:template>
<xsl:template match="break">
<br />
</xsl:template>
</xsl:stylesheet>



Use this on well-formed XML (your input above is not well-formed and cannot be used as input for XSLT) and you get this (I chose XML, but you may want to use HTML as output method):


<p>This is a sentence<br/>
   <img src="http://site.com/image/image.jpg/"/>
  <br/>
       And a second sentence.
</p>



I know that a straight <xsl:value-of...> and <xsl:copy-of...> will not work.

I wonder, what do you mean by "straight"? Because you can use very straightly value-of and copy-of, as long as you use it at the right spot. In my example above, I chose a different approach. If you show what you tried yourself, we can help you better getting it right.


HtH,

Cheers,
-- Abel
  http://www.nuntia.nl

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.