XPath question: [ ] and attributes

K

Karsten Weinert

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>
 
P

Patrick TJ McPhee

% 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.
 
D

Dimitre Novatchev

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 said:
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>
 
K

Karsten Weinert

% <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.
 
K

Karsten Weinert

% <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.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top