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