XSLT match question

Rainer Herbst

Usenet Archive
Joined
Sep 26, 2003
Messages
4
Reaction score
0
Hi *,

please consider the following problem:
I have a XML document which includes some html elements. I want to replace only the <div> element:

I specified two templates, one matches everything ("*"), one matches only the "div". As far as I understand, the most specific rule should apply, i.e. the div rule if the element is a <div>.

But the div rule is never applied! Where is my mistake?

TIA!
Rainer Herbst

The XML document:
<?xml version="1.0" encoding="UTF-8"?>
<page>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="HTML Tidy, see www.w3.org" name="generator"/> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/> <meta content="index" name="robots"/> <meta content="Universitaet Potsdam" name="keywords"/> <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/> <meta content="FrontPage.Editor.Document" name="ProgId"/> <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut icon"/> <title>Homepage der Universität Potsdam</title> </head> <body vlink="#808080" link="#336699" bgcolor="#ffffff"> <div>wrong!!!</div> <p align="center">unterschrift</p> </body> </html> <dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0" name="pressemitteilungen" lastModified="1064833858000" date="29.09.03 13:10" size="4096" sort="name" reverse="false" requested="true"> <dir:file name="first.xml" lastModified="1064833858000" date="29.09.03 13:10" size="62"/> </dir:directory> </page>

My stylesheet:

<?xml version="1.0" standalone="no"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dir="http://apache.org/cocoon/directory/2.0" >

<!-- all elements -->
<xsl:template match="*">
<xsl:copy select=".">
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="div">
<xsl:for-each
select="/page/dir:directory[@name='pressemitteilungen']/dir:file"> <xsl:value-of select="@name"/> </xsl:for-each> </xsl:template>

<xsl:template match="/page">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="dir:directory">
</xsl:template>

</xsl:stylesheet>
 

Francesc Guim Bernat

Usenet Archive
Joined
Jul 29, 2003
Messages
12
Reaction score
0
Rainer said:
Hi *,

please consider the following problem:
I have a XML document which includes some html elements. I want to replace only the <div> element:

I specified two templates, one matches everything ("*"), one matches only the "div". As far as I understand, the most specific rule should apply, i.e. the div rule if the element is a <div>.

That's not true, not the most specific rule is applied. The first rule applied is the firts rule that is declared, for instance, in your sample the first rule in that the expression match results true is:

<xsl:template match="*">

on the other hand you can declare a template with a specific priority (with attribute priority). In this case, nor the first tempalte is applied, the template with more priority is applied, in your sample if you daclare that the second template has more priority this will be applied.

Francesc Guim Bernat

http://francesc.guim.net

But the div rule is never applied! Where is my mistake?

TIA!
Rainer Herbst

The XML document:
<?xml version="1.0" encoding="UTF-8"?>
<page>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="HTML Tidy, see www.w3.org" name="generator"/> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/> <meta content="index" name="robots"/> <meta content="Universitaet Potsdam" name="keywords"/> <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/> <meta content="FrontPage.Editor.Document" name="ProgId"/> <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut icon"/> <title>Homepage der Universität Potsdam</title> </head> <body vlink="#808080" link="#336699" bgcolor="#ffffff"> <div>wrong!!!</div> <p align="center">unterschrift</p> </body> </html> <dir:directory xmlns:dir="http://apache.org/cocoon/directory/2.0" name="pressemitteilungen" lastModified="1064833858000" date="29.09.03 13:10" size="4096" sort="name" reverse="false" requested="true"> <dir:file name="first.xml" lastModified="1064833858000" date="29.09.03 13:10" size="62"/> </dir:directory> </page>

My stylesheet:

<?xml version="1.0" standalone="no"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dir="http://apache.org/cocoon/directory/2.0" >

<!-- all elements -->
<xsl:template match="*">
<xsl:copy select=".">
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="div">
<xsl:for-each
select="/page/dir:directory[@name='pressemitteilungen']/dir:file"> <xsl:value-of select="@name"/> </xsl:for-each> </xsl:template>

<xsl:template match="/page">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="dir:directory">
</xsl:template>

</xsl:stylesheet>
 

A. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
Hi *,

please consider the following problem:
I have a XML document which includes some html elements. I want to replace only the <div> element:

I specified two templates, one matches everything ("*"), one matches only the "div". As far as I understand, the most specific rule should apply, i.e. the div rule if the element is a <div>.

But the div rule is never applied! Where is my mistake?

TIA!
Rainer Herbst

The XML document:
<?xml version="1.0" encoding="UTF-8"?>

The document is not encoded in UTF-8. See later comment.

Here you give this html element and it sub-elements a namespace. The matches in the XSL must be on the namespace qualified element name.
<head>
<meta content="HTML Tidy, see www.w3.org" name="generator"/> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/> <meta content="index" name="robots"/> <meta content="Universitaet Potsdam" name="keywords"/> <meta content="Microsoft FrontPage 4.0" name="GENERATOR"/> <meta content="FrontPage.Editor.Document" name="ProgId"/> <link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut icon"/> <title>Homepage der Universität Potsdam</title>

The above line has a non UTF-8 sequence of bytes after "Universit".
</head>
<body vlink="#808080" link="#336699" bgcolor="#ffffff"> <div>wrong!!!</div> <p align="center">unterschrift</p> </body> </html> [snip]

My stylesheet:

<?xml version="1.0" standalone="no"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dir="http://apache.org/cocoon/directory/2.0" >

<!-- all elements -->
<xsl:template match="*">
<xsl:copy select=".">

Note: the copy element does not have a select attribute.
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="div">

This does not match the div elements in the document because the div elements in the document are in a namespace. You need to match on something like xhtml:div where the xhtml: prefix is associated with the correct namespace using an attribute like

xmlns:xhtml="http://www.w3.org/1999/xhtml"
[snip]
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
%
%
% Rainer Herbst wrote:

% > one matches only the "div". As far as I understand, the most specific % > rule should apply, i.e. the div rule if the element is a <div>.

% That's not true, not the most specific rule is applied. The first rule % applied is the firts rule that is declared, for instance, in your sample % the first rule in that the expression match results true is:
%
% <xsl:template match="*">
%
% on the other hand you can declare a template with a specific priority

If you don't apply a priority, one will be applied for you. The effect of this implicit priority is, roughly speaking, that a more specific rule will be applied over a more general rule, assuming they both match the same node.

If you have two templates which have the same priority, and they match the same node, you have an error, although processors are allowed to deal with it. I think they generally take the last template defined.
 

Francesc Guim Bernat

Usenet Archive
Joined
Jul 29, 2003
Messages
12
Reaction score
0
If you don't apply a priority, one will be applied for you. The effect of this implicit priority is, roughly speaking, that a more specific rule will be applied over a more general rule, assuming they both match the same node.

I'm not agree with you, is possible that i'm wrong, but i'll expose my arguments. I've this xml document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<node name="MyName">
Text
</node>
<node name="YourName">
Text
</node>
</root>

and i apply this simple template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="root"> <newRoot> <xsl:for-each select="node"> <xsl:apply-templates select="."/> </xsl:for-each> </newRoot> </xsl:template> <xsl:template match="node"> <msg> I'm the less specific template </msg> </xsl:template> <xsl:template match="node[name='MyName']"> <msg> I'm one of the most specific template (MyName) </msg> </xsl:template> <xsl:template match="node[name='YourName']"> <msg> I'm one of the most specific template (Your name) </msg> </xsl:template> </xsl:stylesheet>

Where the second template is less specific than the third and fourth template. The result of apply this last stylesheet on the first xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format"> <msg> I'm the less specific template </msg> <msg> I'm the less specific template </msg> </newRoot>

in this sample the less specific rule have been applied, having both templates the same priority. Then i think that nor the most spicific rule is applied , is applied the firts one (with the same priority).

Francesc Guim

http://francesc.guim.net
 

Rainer Herbst

Usenet Archive
Joined
Sep 26, 2003
Messages
4
Reaction score
0
Francesc said:
If you don't apply a priority, one will be applied for you. The effect of this implicit priority is, roughly speaking, that a more specific rule will be applied over a more general rule, assuming they both match the same node.

I'm not agree with you, is possible that i'm wrong, but i'll expose my arguments. I've this xml document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<node name="MyName">
Text
</node>
<node name="YourName">
Text
</node>
</root>

and i apply this simple template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="root"> <newRoot> <xsl:for-each select="node"> <xsl:apply-templates select="."/> </xsl:for-each> </newRoot> </xsl:template> <xsl:template match="node"> <msg> I'm the less specific template </msg> </xsl:template> <xsl:template match="node[name='MyName']"> <msg> I'm one of the most specific template (MyName) </msg> </xsl:template> <xsl:template match="node[name='YourName']"> <msg> I'm one of the most specific template (Your name) </msg> </xsl:template> </xsl:stylesheet>

Where the second template is less specific than the third and fourth template. The result of apply this last stylesheet on the first xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format"> <msg> I'm the less specific template </msg> <msg> I'm the less specific template </msg> </newRoot>

in this sample the less specific rule have been applied, having both templates the same priority. Then i think that nor the most spicific rule is applied , is applied the firts one (with the same priority).

Francesc Guim

http://francesc.guim.net
shouldn't it be [@name='MyName']?
 

Francesc Guim Bernat

Usenet Archive
Joined
Jul 29, 2003
Messages
12
Reaction score
0
Yes, my StyleSheet is wrong. You're in the correct way,i don't know why, but i thought that i had red what i was saying in some XLST book, and of course i didn't. Thanks for your replies.

Francesc Guim

pd: the correct output was :

<?xml version="1.0" encoding="UTF-8"?><newRoot
xmlns:fo="http://www.w3.org/1999/XSL/Format"><msg> I'm one of the most specific template (MyName) </msg><msg> I'm one of the most specific template (Your name) </msg></newRoot>

Rainer said:
Francesc said:
If you don't apply a priority, one will be applied for you. The effect of this implicit priority is, roughly speaking, that a more specific rule will be applied over a more general rule, assuming they both match the same node.

I'm not agree with you, is possible that i'm wrong, but i'll expose my arguments. I've this xml document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<node name="MyName">
Text
</node>
<node name="YourName">
Text
</node>
</root>

and i apply this simple template:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="root"> <newRoot> <xsl:for-each select="node"> <xsl:apply-templates select="."/> </xsl:for-each> </newRoot> </xsl:template> <xsl:template match="node"> <msg> I'm the less specific template </msg> </xsl:template> <xsl:template match="node[name='MyName']"> <msg> I'm one of the most specific template (MyName) </msg> </xsl:template> <xsl:template match="node[name='YourName']"> <msg> I'm one of the most specific template (Your name) </msg> </xsl:template> </xsl:stylesheet>

Where the second template is less specific than the third and fourth template. The result of apply this last stylesheet on the first xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<newRoot xmlns:fo="http://www.w3.org/1999/XSL/Format"> <msg> I'm the less specific template </msg> <msg> I'm the less specific template </msg> </newRoot>

in this sample the less specific rule have been applied, having both templates the same priority. Then i think that nor the most spicific rule is applied , is applied the firts one (with the same priority).

Francesc Guim

http://francesc.guim.net
shouldn't it be [@name='MyName']?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top