473,503 Members | 5,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: probably beginner-type question

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yo!

Mostly as a finger-exercise (and because I'm annoyed again and again how
bad the existing solutions are), I'm hacking up a web-based forum (yes,
the 64832th one, I know).

I want to allow some simplified HTML as input language, and use some xsl
to render that afterward. I do simple verification with a DTD (no
problem in that), but the rendering into html is where I'm stuck
currently.

The articles are stored as:
=====
<article>
<head>
<author>squiggle</author>
<title>Quack</title>
<date>2006-02-26 15:07:12</date>
</head>
<body>Says the Duck.</body>
</article>
=====

The body can be plain text, but mostly it should be simple HTML. I
decided on the following feature set: <em>, <tt>, <p>, <a href="">,
<img alt="" src=""> should be passed through unchanged. <title> should
be changed to <h3>, and <blockquote cite="url"> should be changed to
something like <div><blockquote cite="url">'the
quote'</blockquote><div><a href="url">url</a></div></div>, if an URL is
present, or only blockquote else.

The whole should be wrapped within some <div>'s, showing article title,
author, date etc.

Now I've written some small piece of XSL:
=====
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<!-- title becomes h3 -->
<xsl:template match="title">
<h3><xsl:apply-templates/></h3>
</xsl:template>

<!-- blockquote gets visible citation -->
<xsl:template match="blockquote">
<xsl:element name="blockquote">
<xsl:attribute name="cite">
<xsl:value-of select="@cite"/>
</xsl:attribute>
<div class="text"><xsl:apply-templates/></div>
<div class="cite">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="@cite"/>
</xsl:attribute>
<xsl:value-of select="@cite"/>
</xsl:element>
</div>
</xsl:element>
</xsl:template>

<!-- a, em, img, p, tt go through unchanged -->
<xsl:template match="a|em|img|p|tt">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!-- the complete head is stripped out -->
<xsl:template match="head">
</xsl:template>
</xsl:stylesheet>
=====
And I'm right now extracting article titles etc. manually from the perl
code, and calling the xsl only on the body element. But this is not
really what I want... The point where I'm stuck is with the rules
which part of the xsl is called at which point on which part of the xml
file. And also: I'll certainly need to filter the img and a tags,
because I *only* want to allow the attributes I've talked about above.
(OTOH this is already taken care of by the DTD, so maybe I just won't
care.)

Pointers welcome, but most of the tutorials I've found so far stopped
where things got really interesting. Or maybe I just looked at the
problem from the wrong angle...

anyway, thanks in advance
- -- vbi
- --
Today is Prickle-Prickle, the 26th day of Discord in the YOLD 3172

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: get my key from http://fortytwo.ch/gpg/92082481

iKcEARECAGcFAkQ4zdRgGmh0dHA6Ly9mb3J0eXR3by5jaC9sZW dhbC9ncGcvZW1h
aWwuMjAwMjA4MjI/dmVyc2lvbj0xLjUmbWQ1c3VtPTVkZmY4NjhkMTE4NDMyNzYw
NzFiMjVlYjcwMDZkYTNlAAoJEIukMYvlp/fW834Anjg2J24MXgq/pitFzwHh9wPr
6q8NAKDCeA4etmeGEunQye/BAqQbj17n+w==
=2/6v
-----END PGP SIGNATURE-----
Apr 9 '06 #1
4 1503


Adrian von Bidder wrote:
The point where I'm stuck is with the rules
which part of the xsl is called at which point on which part of the xml
file.
Well first of all if you want HTML output then use
<xsl:output method="html" />
(unless you would create a complete HTML document and had a result html
root element).
As for the rules processing starts at the root node, looks for a
template and takes it from there. There are built in templates which
simply recursively process child nodes.
If you want to control that you can do e.g.
<xsl:template match="article">
and in the template process the nodes you want to process e.g.
<xsl:apply-templates select="head" />
<xsl:apply-templates select="body" />
</xsl:template>
Of course you can put in literal result elements (e.g. div) anywhere in
the template too.
And also: I'll certainly need to filter the img and a tags,
because I *only* want to allow the attributes I've talked about above.


Example
<xsl:template match="img">
<xsl:copy>
<xsl:apply-templates select="@alt | @src" />
</xsl:copy>
</xsl:template>
(Then you need a template for attributes to copy themselves.)

<xsl:template match="a">
<xsl:copy>
<xsl:apply-templates select="@href | node()" />
</xsl:copy>
</xsl:template>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 9 '06 #2
Adrian von Bidder wrote:
And I'm right now extracting article titles etc. manually from the perl
code, and calling the xsl only on the body element. But this is not
really what I want...
Define a template that matches "/" (the root of the document), and begin
processing from there ... for example, by invoking apply-templates
selecting //body, if that's what you want. (The root template is
generally also responsible for producing the root element of the output
document - the <html> element in this case.)

The point where I'm stuck is with the rules
which part of the xsl is called at which point on which part of the xml
file.
The rule is that apply-template invokes the template whose match
attribute matches the node it's being applied to. There are a few
(deliberately) EXTREMELY primitive rules for breaking conflicts, but
mostly it's your responsiblity to avoid letting conflicts and/or to
explicitly specify priority so the system knows which template to select.
And also: I'll certainly need to filter the img and a tags,
because I *only* want to allow the attributes I've talked about above.
(OTOH this is already taken care of by the DTD, so maybe I just won't
care.)


Create a more specific template for those, and rather than doing
apply-templates against @* (all attributes) do it only for the
attributes you want to copy through. Then do the apply against node() to
continue processing with the children.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 9 '06 #3
Adrian von Bidder wrote:
Mostly as a finger-exercise (and because I'm annoyed again and again how
bad the existing solutions are), I'm hacking up a web-based forum (yes,
the 64832th one, I know).

I want to allow some simplified HTML as input language, and use some xsl
to render that afterward. I do simple verification with a DTD (no
problem in that), but the rendering into html is where I'm stuck
currently.

The articles are stored as:
=====
<article>
<head>
<author>squiggle</author>
<title>Quack</title>
<date>2006-02-26 15:07:12</date>
</head>
<body>Says the Duck.</body>
Don't do that. Put the text into at least something like <p></p>
Otherwise it will be impossible to infer it afterwards.
</article>
=====

The body can be plain text, but mostly it should be simple HTML. I
decided on the following feature set: <em>, <tt>, <p>, <a href="">,
<img alt="" src=""> should be passed through unchanged. <title> should
be changed to <h3>, and <blockquote cite="url"> should be changed to
something like <div><blockquote cite="url">'the
quote'</blockquote><div><a href="url">url</a></div></div>, if an URL is
present, or only blockquote else.

The whole should be wrapped within some <div>'s, showing article title,
author, date etc.

Now I've written some small piece of XSL:
=====
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<!-- title becomes h3 -->
<xsl:template match="title">
<h3><xsl:apply-templates/></h3>
</xsl:template>

<!-- blockquote gets visible citation -->
<xsl:template match="blockquote">
<blockquote cite="{@cite}">
....
<xsl:template match="blockquote">
<xsl:element name="blockquote">
<xsl:attribute name="cite">
<xsl:value-of select="@cite"/>
</xsl:attribute>
<div class="text"><xsl:apply-templates/></div>
<div class="cite">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="@cite"/>
</xsl:attribute>
<xsl:value-of select="@cite"/>
</xsl:element>
<a href="{@cite}">
</div>
</xsl:element>
</xsl:template>

<!-- a, em, img, p, tt go through unchanged -->
<xsl:template match="a|em|img|p|tt">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!-- the complete head is stripped out -->
<xsl:template match="head">
</xsl:template>
Do that and it will never see author, title, or date.
</xsl:stylesheet>
=====
And I'm right now extracting article titles etc. manually from the perl
code, and calling the xsl only on the body element. But this is not
really what I want... The point where I'm stuck is with the rules
which part of the xsl is called at which point on which part of the xml
file. And also: I'll certainly need to filter the img and a tags,
If you think that tags are the same as elements, you may already be
in trouble. See http://xml.silmaril.ie/authors/makeup/
because I *only* want to allow the attributes I've talked about above.
(OTOH this is already taken care of by the DTD, so maybe I just won't
care.)


You're doing well, but you have to get the data model right first.
XSLT (using SAX) triggers a matching template (if any) for each
node it encounters, starting at the root node and processing as
deep as it can go before surfacing for air and going down again.
In your example this means the element nodes will be presented for
matching in the order article, head, author, title, date; and then
body, etc. If you explicitly specify no actions for a template,
then nothing further in any node that matches it will ever get
processed, unless it is referenced from some other template.
If no matching template is found for a node, its contents are
in effect fed back through the works, and the process continues,
acting on templates or passing down to the next level, until
nothing is left but parsed character data (PCDATA), which is
output as-is.

[Actually there is more to it than that, but that'll do]

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Apr 9 '06 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[...]

Martin, Joe, Peter - thanks for your input. I'll have a go at this when
I get time and come back with the next step in due time... :-)

(Right now: does the term 'cargo cult programming' tell you something?
I admit freely that I'm at this stage where xslt is concerned ;-)

cheers
- -- vbi

- --
get my gpg key here: http://fortytwo.ch/gpg/92082481

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: get my key from http://fortytwo.ch/gpg/92082481

iKcEARECAGcFAkQ77kZgGmh0dHA6Ly9mb3J0eXR3by5jaC9sZW dhbC9ncGcvZW1h
aWwuMjAwMjA4MjI/dmVyc2lvbj0xLjUmbWQ1c3VtPTVkZmY4NjhkMTE4NDMyNzYw
NzFiMjVlYjcwMDZkYTNlAAoJEIukMYvlp/fWma0AnRn67hT7xpwH17xezaIq65Hv
zUBOAJ9lCZuHRZSDG44pnr/NacfeljvWhA==
=8bpm
-----END PGP SIGNATURE-----
Apr 11 '06 #5

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

Similar topics

1
2327
by: Joachim Spoerhase | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am a XSLT-beginner and i read the XSLT-recommendation of the W3C through. But I did'nt really understand section 5.5 of the latest...
9
4228
by: Christian Roth | last post by:
Hello, when using this "identity" processing sheet: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="iso-8859-1" /> ...
4
1404
by: timothy ma and constance lee | last post by:
do you know any good xslt book
5
4393
by: shauldar | last post by:
Is there a way (tool, hack...) to create an XSL:FO from an XSLT + XML files? My motivation is that we want to use a tool to design reports, and from that "design" generate both HTML (via XSLT)...
12
3381
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...
2
5302
by: solex | last post by:
Hello, Is there a way to develop a style sheet that runs on the client that will open the file in excel? Thanks, Dan
5
1418
by: hilz | last post by:
Hi all I have this situation where I have an xml file similar to this: <Root> <MyElement year="2004"><Amount>10</Amount></MyElement> <MyElement year="2004"><Amount>11</Amount></MyElement>...
3
1996
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
4
2128
by: simon.a.hulbert | last post by:
Hi, I'm trying to view the following xslt transformation using firefox <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method = "xml" indent =...
2
2614
by: RolfK | last post by:
Dear ALL, I need some help on xsl:sequence. I'm using the Altova XSLT processor, but I'm quite confident it is not an processor issue. It's probably my bad knowledge of xslt 2.0. I have probably...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.