File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Does XSLT template declaration order matter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Does XSLT template declaration order matter?" Watch "Does XSLT template declaration order matter?" New topic
Author

Does XSLT template declaration order matter?

kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
Hi,
Does the order of declaring templates in XML has any significance in processing order of these templates? My XML contains templates in following oder:

If the source html contains <u><strong><em> tag in same order as in XML the tags are processed correctly, but if the order differs, the templates are not processed correctly. Say if order is <u><em><strong> then the em template is processed twice. I think am making some fundamental mistake here as I haven't read anywhere that templates are processed in the order they are declared.
Thanks,
Kapil
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Well, as I read section 5.5 of the XSTL Recommendation, all of those three templates have the same priority (because they are all of the same form). So the rule for choosing among the three is this:
It is an error if this leaves more than one matching template rule. An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from amongst the matching template rules that are left, the one that occurs last in the stylesheet.
So yes, the order of the templates does matter.
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
Ok, But What I could not understand is if the source content contains a tag say, strong and there is a template defined with match="strong//text()", then why the processor tries to process template with maximum priority and if the priority is same then it processes the last one again?
regards,
kapil
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

I have no idea. I can't even see why it works the way you say it does. As far as I can see, the first thing that should happen is that the pattern "u//text()" matches a text node, and you produce a <RICHTEXT UNDERLINE="true"> element containing that text. From there on the xsl:apply-templates shouldn't match anything, because the text node doesn't have any children to apply to.

Edit: You said "If the source html contains <u><strong><em> tag..." and I took that to mean exactly what it said. That is, I took it to mean that you didn't mention the end tags and you had something like <u><strong><em>some text</em></strong></u>. But it's possible that you were really talking about elements and just using sloppy terminology calling them tags. In that case you would have something like <u>some text</u><strong>some text</strong><em>some text</em>. In which case your templates make a bit more sense.

Okay. Let's go with the misleading terminology idea. All your templates say is "When you see a <u> element do this" and so on. So there aren't any instances when two of those templates will match a single text node, so what I said in my first reply is irrelevant.

The first template (span//text()) does nothing except to prevent span elements from producing anything in the output. Calling xsl:apply-templates in it does nothing because the text node it selects can't have any children.

So I don't see where the other templates are being called from. Possibly they are being called via the build-in processing rules.
[ April 20, 2007: Message edited by: Paul Clapham ]
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
The input is basically HTML and it is in the form of

So basically a span may contain n number of strong, em, strike tags in any order and I have to convert it into XML of the form

So if both strong and italic is applied to a text, I have to set two attribute s of richtext node.
The complete XSL is:

The other templates for em, strike etc are same as strong//text()

[ April 20, 2007: Message edited by: kapil Gupta ]
[ April 20, 2007: Message edited by: kapil Gupta ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18136
    
    8

Originally posted by kapil Gupta:
So if both strong and italic is applied to a text, I have to set two attribute s of richtext node.
Well, the XSLT you posted isn't going to do that regardless of your questions about the order of things. It's going to generate one RICHTEXT element for each strong element in the input, and one RICHTEXT element for each em element in the input. Or at most it will generate that, depending on whether your xsl:apply-templates even finds those elements. There's no way it can generate one RICHTEXT element with two attributes, if that was your intention.

And by the way when your template matches a text node (like all the ones you posted do) there is no point in calling xsl:apply-templates inside it, because the text node has no children to apply templates to anyway.
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
For creating two attribute am using the ancestor axis to get all nodes and iterating over them to check names.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does XSLT template declaration order matter?
 
Similar Threads
xsl getNext to sort output
[XSL-FO][docbook]Problem print line in table of contents
How to handle nested html tags in XSL?
XSLT apply-template with-param not working
XSL fails with xmlns