473,403 Members | 2,284 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,403 software developers and data experts.

XSLT match question

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>

--
------------------------------------------------
Rainer Herbst Linux - Registered
ZEIK User #319157
Universität Potsdam Usual disclaimers applied!
------------------------------------------------

Jul 20 '05 #1
6 4410


Rainer Herbst wrote:
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>


Jul 20 '05 #2
In article <bl*********@zeppelin.rz.uni-potsdam.de>, Rainer Herbst wrote:
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.
<page>
<html xmlns="http://www.w3.org/1999/xhtml">
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]
Jul 20 '05 #3
In article <3F**************@fib.upc.es>,
Francesc Guim Bernat <fg***@fib.upc.es> wrote:
%
%
% 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.

--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #4
>
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

Jul 20 '05 #5
Francesc Guim Bernat schrieb:

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']?
--
------------------------------------------------
Rainer Herbst Linux - Registered
ZEIK User #319157
Universität Potsdam Usual disclaimers applied!
------------------------------------------------

Jul 20 '05 #6
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 Herbst wrote:
Francesc Guim Bernat schrieb:

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']?


Jul 20 '05 #7

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

Similar topics

1
by: David Way | last post by:
I have an xml file looks like this: <root> <L1>a</L1> <L2>b <L3>c</L3> </L2> </root> In my xslt file, I do a template match to get to <L3>
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
7
by: pintihar | last post by:
Hi, As a follow on from an earlier post I have another question about xslt. Is it possible to create the stylsheet programatically? Is this sensible? In the first phase I needed to map element...
5
by: Patrick.O.Ige | last post by:
I have an xml and i'm trying to loop each node... When i do FOR EACH i seem not to get my desired result I want to loop through and get only the values that matches the question i specified with...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
3
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...
0
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
8
by: Hercules Dev. | last post by:
Hi all, I'm new in xslt and xpath, so my question might be simple but i'm learning. I have an XML document and need to transform it into another XML, I use xslt and it works, but there is a...
2
by: saritha2008 | last post by:
Hi, As part of transforming one form of xml to another form, i need to do the below mentioned transformation: My Input XML: <rss> <channel> <item> <assignee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.