xsl-list
[Top] [All Lists]

RE: [xsl] Processing inner elements

2006-12-29 19:02:34
Thanks Abel,

That's exactly the sort of construct that I was referring to. I guess that
my problem was trying to work out where to look for that sort of
information. For example, what search words would you throw at Google to try
and find pages holding that info?

My apologies for not having written clean XML and posting it here and for
using the word "obvious". You interpreted obvious perfectly but in the
future I will be explicit an not leave anything to assumption.

Many thanks for your time and help. Also thanks to the others for their
posts. I won't clutter the list by publicly replying to each of you.

Baie dankie!
Guy

-----Original Message-----
From: Abel Braaksma [mailto:abel(_dot_)online(_at_)xs4all(_dot_)nl] 
Sent: Friday, December 29, 2006 4:35 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Processing inner elements

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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>