473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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 >
<xsl:otherwise>
<!-- process text node -->
</xsl:otherwise>
</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
Jul 20 '05 #1
2 2762
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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" <Ra*********@iwr.uni-heidelberg.de> wrote in message
news:6a**************************@posting.google.c 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 >
<xsl:otherwise>
<!-- process text node -->
</xsl:otherwise>
</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

Jul 20 '05 #2
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
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
5
by: Ruthless | last post by:
hello. All XML and XSLT are processed by preprocessor as a trees. How can i simply display my XML as some kind of tree. given xml: <struct> <node level="1" no="1">
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
0
by: alex | last post by:
I'm new to xslt, and I am attempting to use it to produce a comma-separated-value file from a large, dynamically-generated data file formatted in xml (examples of the xml file and my xslt style...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
3
by: Carles Company Soler | last post by:
Hello, I want to calculate the value of an attribute. For example <rect x="2+3" y="12"and be <rect x="5" y="12">. Is it possible using XSLT? Thanks!
3
by: abhishek.smu | last post by:
Given an XML like: <root> <node>8</node> <node>21</node> <node>-7</node> <node>13</node> <node>43</node> <node>2</node> </root>
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.