Simple Question

M

Mike King

What is the XPath expression that selects the value attribute in the
following xml?

<?xml version="1.0"?>
<root xmlns="http://a/" xmlns:a="http://a/">
<child a:value="1"/>
</root>
 
J

Joris Gillis

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://a/">
<xsl:template match="@a:value">
found
</xsl:template>
</xsl:stylesheet>

The problem here is that attribute nodes are not applied by default.
An equivalent of the built-in template rule in XSLT1.0:
<xsl:template match="*|/">
<xsl:apply-templates select="node()"/>
</xsl:template>

In order to let your xslt work, add this template:
<xsl:template match="*|/">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>
but this will likely result in other unwanted behaviour.


regards,
 
D

David Carlisle

I tried that, and it didn't work. What's wrong with my XSLT.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://a/">
<xsl:template match="@a:value">
found
</xsl:template>
</xsl:stylesheet>

A template only does anything if you apply templates to the matching
node, and that stylesheet doesn't apply templates to attribute nodes, so
no template natching an attribute will be executed.

the default templates for / and elements apply templates to child nodes
(only) not to attribute or namespace nodes. This is why an empty
stylesheet produces all element content (but not attribute content)

add

<xsl:template match="*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>

David
 
M

Mike King

A template only does anything if you apply templates to the matching
node, and that stylesheet doesn't apply templates to attribute nodes, so
no template natching an attribute will be executed.

the default templates for / and elements apply templates to child nodes
(only) not to attribute or namespace nodes. This is why an empty
stylesheet produces all element content (but not attribute content)

add

<xsl:template match="*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>

David

Ok then won't the following match as a template?

a:child/@a:value
 
J

Joris Gillis

Ok then won't the following match as a template?

a:child/@a:value


No,

The pattern '@a:value' isn't matched, so the same pattern with an extra
restriction (only 'value' attributes with a 'child' parent) will certainly
not match more.

But you can do this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="s" xmlns:w="vv" xmlns:abc="abc"
version="1.0" xmlns:a="http://a/">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="a:root">
<xsl:apply-templates select="a:child/@a:value"/>
</xsl:template>

<xsl:template match="a:child/@a:value ">
match
</xsl:template>

</xsl:stylesheet>
 
M

Mike King

No,
The pattern '@a:value' isn't matched, so the same pattern with an extra
restriction (only 'value' attributes with a 'child' parent) will certainly
not match more.

But you can do this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="s" xmlns:w="vv" xmlns:abc="abc"
version="1.0" xmlns:a="http://a/">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="a:root">
<xsl:apply-templates select="a:child/@a:value"/>
</xsl:template>

<xsl:template match="a:child/@a:value ">
match
</xsl:template>

</xsl:stylesheet>


I'm starting to understand now. XSLT is like a push-model where the
XSL/XSLT processor *must* be told to look for a specific pattern, not that
it's recursively going through the tree structure looking for a matching
pattern.
 
J

Joris Gillis

I'm starting to understand now. XSLT is like a push-model where the
XSL/XSLT processor *must* be told to look for a specific pattern, not
that it's recursively going through the tree structure looking for a
matching
pattern.

The xslt processor has to be told (with <xsl:apply-templates/>) to dive
further into the source tree, but there are built in templates.
http://www.w3.org/TR/xslt.html#built-in-rule
The built-in templates make the XLST processor go through all nodes,
except attribute nodes.
 

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

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top