473,763 Members | 5,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Univer sitaet Potsdam" name="keywords"/>
<meta content="Micros oft FrontPage 4.0" name="GENERATOR "/>
<meta content="FrontP age.Editor.Docu ment" name="ProgId"/>
<link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
icon"/>
<title>Homepa ge der Universität Potsdam</title>
</head>
<body vlink="#808080" link="#336699" bgcolor="#fffff f">
<div>wrong!!! </div>
<p align="center"> unterschrift</p>
</body>
</html>
<dir:director y xmlns:dir="http ://apache.org/cocoon/directory/2.0"
name="pressemit teilungen" lastModified="1 064833858000" date="29.09.03
13:10" size="4096" sort="name" reverse="false" requested="true ">
<dir:file name="first.xml " lastModified="1 064833858000" date="29.09.03
13:10" size="62"/>
</dir:directory>
</page>
My stylesheet:

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

<xsl:styleshe et 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:attribut e 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='pressemi tteilungen']/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:dire ctory">
</xsl:template>

</xsl:stylesheet>

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

Jul 20 '05 #1
6 4442


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="Univer sitaet Potsdam" name="keywords"/>
<meta content="Micros oft FrontPage 4.0" name="GENERATOR "/>
<meta content="FrontP age.Editor.Docu ment" name="ProgId"/>
<link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
icon"/>
<title>Homepa ge der Universität Potsdam</title>
</head>
<body vlink="#808080" link="#336699" bgcolor="#fffff f">
<div>wrong!!! </div>
<p align="center"> unterschrift</p>
</body>
</html>
<dir:director y xmlns:dir="http ://apache.org/cocoon/directory/2.0"
name="pressemit teilungen" lastModified="1 064833858000" date="29.09.03
13:10" size="4096" sort="name" reverse="false" requested="true ">
<dir:file name="first.xml " lastModified="1 064833858000" date="29.09.03
13:10" size="62"/>
</dir:directory>
</page>
My stylesheet:

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

<xsl:styleshe et 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:attribut e 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='pressemi tteilungen']/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:dire ctory">
</xsl:template>

</xsl:stylesheet>


Jul 20 '05 #2
In article <bl*********@ze ppelin.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="Univer sitaet Potsdam" name="keywords"/>
<meta content="Micros oft FrontPage 4.0" name="GENERATOR "/>
<meta content="FrontP age.Editor.Docu ment" name="ProgId"/>
<link href="http://www.uni-potsdam.de/over/unilogol.ico" rel="shortcut
icon"/>
<title>Homepa ge 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="#fffff f">
<div>wrong!!! </div>
<p align="center"> unterschrift</p>
</body>
</html>
[snip]
My stylesheet:

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

<xsl:styleshe et 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:attribut e 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="ht tp://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.c om
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:styleshe et 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:styleshe et 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:styleshe et 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
1464
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
1832
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
5207
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 name from inbound xml to my internal elements to standardize disparate input. Now I could just create an xslt stylesheet for each possible inbound format and be done, but I think it would be powerful to be able store this mapping in a database and...
5
1603
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 the corresponding answers? I don't want to retrieve all the Answer and VoteAnswers nodes <xsl:for-each select="NSurveyVoter/Voter/Question/Answer"> <xsl:value-of select="Answer/Answer/text()"/>
3
3092
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> <OPTION>3<OPTION> </FIELD>
3
2010
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 processor input. The basic class hierarchy is:
0
3303
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. Each section has a list of questions. Each question has responses. I need to display a list of responses to the questions (i.e. set:distinct), once and only once, each section. My second problem is that these questions can also have corrective...
18
2084
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 look up what each feature depends on and gerenate a text file. For example, in the following file, I would like to find out feature A depends on A1 and A2 and feature B depends on B1 and B2. And write that to a text file.
8
15902
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 case that i don't know how to solve, I need to concat a string from multiple childs into a standard way, the following is an example of the source and the target XML.
2
9162
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 username="srinivas.rachakonda">Srinivas Rachakonda</assignee>
0
9564
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.