Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: branching node processing with respect to node type possible?

Reply
Thread Tools

XSLT: branching node processing with respect to node type possible?

 
 
Ralf Wahner
Guest
Posts: n/a
 
      07-15-2003
Dear Masters of XSLT

Could I ask you for a clue on the following question? I'd
like to use XSLT to transform an XML source file to LaTeX.
In the following small example the <para> Element contains
(I think so)

- a text node (node one)
- an element node (node two)
- a text node (node three)

Example:

<para>
Some words or lines of common text with LaTeX special
characters like $, # and _ (underscore) appear before
an element node.
<emph>Attention!</emph>
Take care for converting $ to \$, # to \# and _ to \_
before running latex.
</para>

I'd like to process the nodes in the <para> element content one
after the other with respect to their node type. For text nodes
it's sufficient just to change all occuring special characters,
while the <emph> element node should get e.g. a \textbf{} or
\emph{} before its content will be inserted (self-evidently after
first changing the special characters). In terms of pseudo-XSLT:

<xsl:template match="para">
<xsl:for-each select="."> <!-- get set of childs of para -->
<xsl:choose>
<xsl:when test="emph">
<!-- process element node -->
</xsl:when >
<xsltherwise>
<!-- process text node -->
</xsltherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Unfortunately, this works only, when the <emph> element node
is cut out from the above example. I spend about five days but
gained no success. I pressume that my point of view is unfavo-
rable: Given the childs of a element node. Take one after the
other and detect the node type. "Node one is an element node,
node two is a processing instruction, ...".

I'm working with XSLT for several months now, but still haven't
understood how to think/to look at a problem to find an approp-
riate solution in XSLT. I'd gratefully appreciate any hint on
the topic at hand as well as on "Thinking in XSLT".

Thank you very much,

Ralf
 
Reply With Quote
 
 
 
 
Marrow
Guest
Posts: n/a
 
      07-15-2003
Hi Ralf,

The best suggestion would be to avoid using <xsl:for-each> in this type of
processing - your <xsl:choose> might get extremely large, complicated and
unwieldy.

When processing nodes where the order is important but yet different
actions/processing is to take place for each different node then
<xsl:apply-templates> and matching templates is usually the best route, e.g.
a very crude example of processing your XML...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>
<xsl:template match="para">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="emph">
<xsl:text>\emph{</xsl:text>
<xsl:apply-templates/>
<xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="text()">
<xsl:text>\textbf{</xsl:text>
<xsl:value-of select="."/>
<xsl:text>}</xsl:text>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator




"Ralf Wahner" <> wrote in message
news: om...
> Dear Masters of XSLT
>
> Could I ask you for a clue on the following question? I'd
> like to use XSLT to transform an XML source file to LaTeX.
> In the following small example the <para> Element contains
> (I think so)
>
> - a text node (node one)
> - an element node (node two)
> - a text node (node three)
>
> Example:
>
> <para>
> Some words or lines of common text with LaTeX special
> characters like $, # and _ (underscore) appear before
> an element node.
> <emph>Attention!</emph>
> Take care for converting $ to \$, # to \# and _ to \_
> before running latex.
> </para>
>
> I'd like to process the nodes in the <para> element content one
> after the other with respect to their node type. For text nodes
> it's sufficient just to change all occuring special characters,
> while the <emph> element node should get e.g. a \textbf{} or
> \emph{} before its content will be inserted (self-evidently after
> first changing the special characters). In terms of pseudo-XSLT:
>
> <xsl:template match="para">
> <xsl:for-each select="."> <!-- get set of childs of para -->
> <xsl:choose>
> <xsl:when test="emph">
> <!-- process element node -->
> </xsl:when >
> <xsltherwise>
> <!-- process text node -->
> </xsltherwise>
> </xsl:choose>
> </xsl:for-each>
> </xsl:template>
>
> Unfortunately, this works only, when the <emph> element node
> is cut out from the above example. I spend about five days but
> gained no success. I pressume that my point of view is unfavo-
> rable: Given the childs of a element node. Take one after the
> other and detect the node type. "Node one is an element node,
> node two is a processing instruction, ...".
>
> I'm working with XSLT for several months now, but still haven't
> understood how to think/to look at a problem to find an approp-
> riate solution in XSLT. I'd gratefully appreciate any hint on
> the topic at hand as well as on "Thinking in XSLT".
>
> Thank you very much,
>
> Ralf



 
Reply With Quote
 
 
 
 
Ralf Wahner
Guest
Posts: n/a
 
      07-15-2003
Dear Mr. Marrow

Thank you very much for your reply. You showed me precisely what I
was looking for. I didn't hit on writing an own template for text
as you proposed:

<xsl:template match="text()">
<xsl:text>\textbf{</xsl:text>
<xsl:value-of select="."/>
<xsl:text>}</xsl:text>
</xsl:template>

My only approach in handling pure text content sofar was
<xsl:value-of select=".">. I take the way you treat beginners like
me as a good example. The more I learn the earlier I can contribute
to news group discussions.

Best regards,

Ralf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type erasure with respect to compile-time vs. runtime parameters Kevin McCarty C++ 0 10-05-2012 06:22 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
Source Control and sharing/branching Matze ASP .Net 2 11-17-2003 11:18 AM
branching based on a datagrid/datalist DataBinder? BH ASP .Net 1 07-21-2003 08:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57