template match, wich one should it be:

  • Thread starter Tjerk Wolterink
  • Start date
T

Tjerk Wolterink

I have xml inpput like this:

<meta:empty/>


And xsl like this:


<xsl:template match="meta:empty">A</xsl:template>
<xsl:template match="//meta:*">B</xsl:template>


The namespaces meta or both bound to the same uri, so no
problem with that.

My Question,
what should be outputted when i input the meta:empty element,
A
or
B

My xsl processor outputs B, but i expect it to output A
because the match pattern "meta:empty" is more specifc then
"//meta:*"

What should it do?
 
P

phil.a.jenkins

I would have expected A to have a higher default priority than B, but
you could try forcing B's priority to a low value to make sure the
first template is being picked up.

i.e.

<xsl:template match="//meta:*" priority="-1">B</xsl:template>
 
T

Tjerk Wolterink

I would have expected A to have a higher default priority than B, but
you could try forcing B's priority to a low value to make sure the
first template is being picked up.

i.e.

<xsl:template match="//meta:*" priority="-1">B</xsl:template>

But what does the W3C say? Wich one should have priority?
 
P

Phil Jenkins

From http://www.w3.org/TR/xslt#conflict

5.5 Conflict Resolution for Template Rules

....

Thus, the most common kind of pattern (a pattern that tests for a node
with a particular type and a particular expanded-name) has priority 0.
The next less specific kind of pattern (a pattern that tests for a node
with a particular type and an expanded-name with a particular namespace
URI) has priority -0.25. Patterns less specific than this (patterns
that just tests for nodes with particular types) have priority -0.5.
Patterns more specific than the most common kind of pattern have
priority 0.5.

.....

So bascially the more specific the match - the higher the default
priority should be
 
T

Tjerk Wolterink

Phil said:
5.5 Conflict Resolution for Template Rules

...

Thus, the most common kind of pattern (a pattern that tests for a node
with a particular type and a particular expanded-name) has priority 0.
The next less specific kind of pattern (a pattern that tests for a node
with a particular type and an expanded-name with a particular namespace
URI) has priority -0.25. Patterns less specific than this (patterns
that just tests for nodes with particular types) have priority -0.5.
Patterns more specific than the most common kind of pattern have
priority 0.5.

....

So bascially the more specific the match - the higher the default
priority should be

So my xsl processor is doing it wrongly?

i'm using Xalan.
 
T

Tjerk Wolterink

I would have expected A to have a higher default priority than B, but
you could try forcing B's priority to a low value to make sure the
first template is being picked up.

i.e.

<xsl:template match="//meta:*" priority="-1">B</xsl:template>

It works with this fix, thanks.

But i want to understand why it did'nt work...
 
P

Phil Jenkins

I'm curious too :)

Try swapping the two original (unfixed) templates around. If A now
occurs instead of B it means that they both have the same priority;
Xalan will pick the template that occurs last if more than one template
has the same priority when matched - if that makes sense!

My experience has always been to explicitly define the priority of any
items that I want to be considered last - i'm relatively new to the
default priority calculations, seems like extra complication having to
rely on the processor to decide.
 
R

Richard Tobin

Tjerk Wolterink said:
<xsl:template match="meta:empty">A</xsl:template>
<xsl:template match="//meta:*">B</xsl:template>

See http://www.w3.org/TR/xslt#conflict

The first pattern has priority 0 because it has the form QName
preceded by a ChildOrAttributeAxisSpecifier.

The second pattern has priority 0.5 because it doesn't fall into any
of the other listed categories. In particular, it isn't of the
form NCName:* preceded by a ChildOrAttributeAxisSpecifier, because
// is not a ChildOrAttributeAxisSpecifier.

So the second rules has higher priority.

The fact that //meta:* matches the same nodes as meta:*, which would
have priority -0.25, is not relevant :)

-- Richard
 
P

Phil Jenkins

Richard said:
See http://www.w3.org/TR/xslt#conflict

The first pattern has priority 0 because it has the form QName
preceded by a ChildOrAttributeAxisSpecifier.

The second pattern has priority 0.5 because it doesn't fall into any
of the other listed categories. In particular, it isn't of the
form NCName:* preceded by a ChildOrAttributeAxisSpecifier, because
// is not a ChildOrAttributeAxisSpecifier.

So the second rules has higher priority.

The fact that //meta:* matches the same nodes as meta:*, which would
have priority -0.25, is not relevant :)

-- Richard

Superb answer - the W3C terminology was predictably obtuse so
appreciate the translation:)
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top