473,387 Members | 1,863 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,387 software developers and data experts.

XPath question: [ ] and attributes

Hello,
can someone please explain to me why the one stylesheet given below
works, but the other not?

<!-- This stylesheet works -->
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">
<html><head></head><body>
<xsl:apply-templates/>
</body></html>
</xsl:template>

<xsl:template match="extern">
<xsl:variable name="vPart" select="@part"/>
<xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
</xsl:template>

</xsl:stylesheet>

<!-- This stylesheet does not work -->
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">
<html><head></head><body>
<xsl:apply-templates/>
</body></html>
</xsl:template>

<xsl:template match="extern">
<xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
</xsl:template>

</xsl:stylesheet>

The stylesheet work on this file:

<?xml version="1.0"?>

<content>
<extern file="extern_resource.xml" part="parta"/>
</content>

and use this file also:

<!-- extern_resource.xml -->

<?xml version="1.0"?>
<content>
<parta>This is the content part a</parta>
<partb>This is the content part b</partb>
</content>
Jul 20 '05 #1
4 1687
In article <5c**************************@posting.google.com >,
Karsten Weinert <es*******@gmx.net> wrote:

% can someone please explain to me why the one stylesheet given below
% works, but the other not?

Inside a predicate, the context node is set to the node against which
the predicate is being tested.

% <xsl:template match="extern">
% <xsl:variable name="vPart" select="@part"/>

Here, vPart is set to the value of the attribute `part' of the element
`extern'.

% <xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
% </xsl:template>

And here, it's tested against the name of each element which is a child
of `content' in the the file given by the `file' attribute of `extern'.

% <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>

Here' the attribute `part' of each element which is a child of `content'
in the file given by the `file' attribute of `extern' is tested against
the name of the same element. That is, `part' is not taken from `extern',
but from the node being tested by the predicate.
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2
Just to add to the precise explanation of Patrick:

You can avoid having to use a variable. Instead of:
<xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
use

<xsl:apply-templates
select="document(@file)/content/*[name()=current()/@part]"/>
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"Karsten Weinert" <es*******@gmx.net> wrote in message
news:5c**************************@posting.google.c om... Hello,
can someone please explain to me why the one stylesheet given below
works, but the other not?

<!-- This stylesheet works -->
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html><head></head><body>
<xsl:apply-templates/>
</body></html>
</xsl:template>

<xsl:template match="extern">
<xsl:variable name="vPart" select="@part"/>
<xsl:apply-templates select="document(@file)/content/*[name()=$vPart]"/>
</xsl:template>

</xsl:stylesheet>

<!-- This stylesheet does not work -->
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html><head></head><body>
<xsl:apply-templates/>
</body></html>
</xsl:template>

<xsl:template match="extern">
<xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>
</xsl:template>

</xsl:stylesheet>

The stylesheet work on this file:

<?xml version="1.0"?>

<content>
<extern file="extern_resource.xml" part="parta"/>
</content>

and use this file also:

<!-- extern_resource.xml -->

<?xml version="1.0"?>
<content>
<parta>This is the content part a</parta>
<partb>This is the content part b</partb>
</content>

Jul 20 '05 #3
pt**@interlog.com (Patrick TJ McPhee) wrote in message news:<bm**********@news.eusc.inter.net>...
% <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>

Here' the attribute `part' of each element which is a child of `content'
in the file given by the `file' attribute of `extern' is tested against
the name of the same element. That is, `part' is not taken from `extern',
but from the node being tested by the predicate.


Ah, it looks for an attribut "part" of the <parta> or <partb> tags,
which do not exists. Thanks a lot,

Karsten.
Jul 20 '05 #4
pt**@interlog.com (Patrick TJ McPhee) wrote in message news:<bm**********@news.eusc.inter.net>...
% <xsl:apply-templates select="document(@file)/content/*[name()=@part]"/>

Here' the attribute `part' of each element which is a child of `content'
in the file given by the `file' attribute of `extern' is tested against
the name of the same element. That is, `part' is not taken from `extern',
but from the node being tested by the predicate.


Ah, it looks for an attribut "part" of the <parta> or <partb> tags,
which do not exists. Thanks a lot,

Karsten.
Jul 20 '05 #5

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

Similar topics

1
by: Chris | last post by:
Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
9
by: DBC User | last post by:
Hi, I have an xml and I am able to use xpath to identify each node that statisfy the selection criteria. I got the node list. I would like to know is it possible to do the following for the...
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
6
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return...
0
by: jeoffh | last post by:
Background: I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete...
0
by: pompair | last post by:
Hello, I'm making a quiz game for fun. I have an xml file like this: <?xml version="1.0" encoding="utf-8" ?> <results> <index>99</index> <answers>11</answers> <questions> <question id="1">
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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
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
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...

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.