You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Torsten Curdt <tc...@dff.st> on 2000/07/19 11:12:36 UTC

xsl matching

just a simple question...
...don't know if got it right from book.
Doesn't seem to work in real life.

I have a XML file:

<page>
  <navigation>
     <item/>
     <item/>
  </navigation>
  <menu>
     <item/>
     <item/>
  </menu>
</page>

Now I have two different items but wanna
match them differently. Thought it was:

<xsl:template match="navigation/item">
 ..
</xsl:template>

<xsl:template match="menu/item">
 ..
</xsl:template>

..but it doesn't seem to work.
--
Torsten


Re: xsl matching

Posted by Ulrich Mayring <ul...@denic.de>.
Torsten Curdt wrote:
> 
> <xsl:template match="navigation/item"/>
> <xsl:template match="menu/item"/>
> 
> ..should do it

XSLT is kind of declarative with some recursion and pattern matching, so
you may be better off thinking like this:

<xsl:template match="navigation|menu">
	<!-- do whatever -->
	<xsl:apply-templates/>
	<!-- do whatever -->
</xsl:template>

<xsl:template match="navitem|menuitem">
	<!-- do whatever -->
	<xsl:apply-templates/>
	<!-- do whatever -->
</xsl:template>

But the definitely best (and hardest) way to think XML is something like
this:

<category name="products">
<product name="Computer">
...
</product>
...
</category>

<category name="links">
<link url="http://xml.apache.org/cocoon" pretext="The homepage of the "
linktext="cocoon" posttext=" project"/>
...
</category>

<category name="documents">
<document name="Terms of Business">
...
</document>
</category>

Then you build your menu and navigation bar from this structure, which
is easily re-usable for other applications as well.

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung

Re: xsl matching

Posted by Mark Washeim <31...@t-online.de>.
on 19/7/00 12:54 pm, Torsten Curdt at tcurdt@dff.st wrote:

>>> just a simple question...
>>> ....don't know if got it right from book.
>>> Doesn't seem to work in real life.
>>> 
>>> I have a XML file:
>>> <page>
>>> <navigation>
>>> <item/>
>>> <item/>
>>> </navigation>
>>> <menu>
>>> <item/>
>>> <item/>
>>> </menu>
>>> </page>
>>> 
>>> Now I have two different items but wanna
>>> match them differently. Thought it was:
>>> 
>>> <xsl:template match="navigation/item">
>>> </xsl:template>
>>> 
>>> <xsl:template match="menu/item">
>>> </xsl:template>
>>> 
>>> ...but it doesn't seem to work.
>> what's the context?
> 
> Well, if I see it from a OOP point of view:
> I have an object navigation and an object menu.
> Both have an attribute named item. But since
> they are items from different objects (which
> is reflected in the hierachic DOM structure)
> they are also wanted to be displayed differently.
> 

Ok. XSL is declarative and, hence, not so well suited to an oo method of
thinking, in my opinion. But, ....

>> Within 
>> <xsl:template match="page">
>> <xsl:apply-templates/>
>> </xsl:template>
>> 
>> <xsl:template match="navigation">
>> <xsl:apply-templates select="item" mode="navigation"/>
>> </xsl:template>
>> 
>> <xsl:template match="item" mode="navigation">
>> ...
>> </xsl:template>
>> 
>> 
>> you can use mode="navigation", mode="menu" if you want different
>> templates:
> 
> This will works... but actually I think it's a bit
> of a work around.
> Shouldn't I be able to match it like I can match
> top level objects via:

It's not a work around. It allows you to keep variants clearly separated
which I think is good practice (rather than having logic in the template to
discern it's context) . . .
 
> <xsl:template match="/toplevelobject">
> 
> This is a match that reflects the hierachic position
> of the object. If not relative... shouldn't it work
> absolute like this ?
> 
> <xsl:template match="/page/navigation/item"/>
> 
> <xsl:template match="/page/menu/item"/>
> 

Ok. use match="//page" to indicate the root (//) and it should work.


> But actually I'd like to have it to match
> for any menu or navigation item - not just
> those exactly inside the page object.
> 
> This is why I thought...
> 
> <xsl:template match="navigation/item"/>
> <xsl:template match="menu/item"/>
> 
> ...should do it
> 
> (Hope this is not too off topic)

This will only work in situ, that is, in the correct context:

so, where current node is page
<xsl:template match="page">
    <xsl:apply-templates select="navigation/item">
</xsl:template>

<xsl:template match="navigation/item">
    ...
</xsl:template>

will work. but, I think that it's not as clean as using mode . . .


> --
> Torsten
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: cocoon-users-help@xml.apache.org
> 

-- 
Mark (Poetaster) Washeim

'On the linen wrappings of certain mummified remains
found near the Etrurian coast are invaluable writings
that await translation.

Quem colorem habet sapientia?'

Evan S. Connell

 



RE: xsl matching

Posted by Ed Staub <es...@mediaone.net>.
Torsten,

I like MarkW's "mode" solution, but if you really want to do this in the way
you're thinking...

While you didn't include the context in your mail, I suspect that the
problem is that you're in the wrong context.

<xsl:template match="navigation/item"> will work when triggered from an
apply-template at the <page> level.  If you are attempting to trigger it
from an apply-template at the <navigation> level, you need to use something
like:
	match="item[parent::navigation]"
instead.

-Ed Staub

-----Original Message-----
From: Torsten Curdt [mailto:tcurdt@dff.st]
Sent: Wednesday, July 19, 2000 6:54 AM
To: cocoon-users@xml.apache.org
Subject: RE: xsl matching


> > just a simple question...
> > ....don't know if got it right from book.
> > Doesn't seem to work in real life.
> >
> > I have a XML file:
> > <page>
> > <navigation>
> > <item/>
> > <item/>
> > </navigation>
> > <menu>
> > <item/>
> > <item/>
> > </menu>
> > </page>
> >
> > Now I have two different items but wanna
> > match them differently. Thought it was:
> >
> > <xsl:template match="navigation/item">
> > </xsl:template>
> >
> > <xsl:template match="menu/item">
> > </xsl:template>
> >
> > ...but it doesn't seem to work.
> what's the context?

Well, if I see it from a OOP point of view:
I have an object navigation and an object menu.
Both have an attribute named item. But since
they are items from different objects (which
is reflected in the hierachic DOM structure)
they are also wanted to be displayed differently.

> Within
> <xsl:template match="page">
>     <xsl:apply-templates/>
> </xsl:template>
>
>  <xsl:template match="navigation">
>     <xsl:apply-templates select="item" mode="navigation"/>
>  </xsl:template>
>
>  <xsl:template match="item" mode="navigation">
>    ...
>  </xsl:template>
>
>
> you can use mode="navigation", mode="menu" if you want different
> templates:

This will works... but actually I think it's a bit
of a work around.
Shouldn't I be able to match it like I can match
top level objects via:

<xsl:template match="/toplevelobject">

This is a match that reflects the hierachic position
of the object. If not relative... shouldn't it work
absolute like this ?

<xsl:template match="/page/navigation/item"/>

<xsl:template match="/page/menu/item"/>

But actually I'd like to have it to match
for any menu or navigation item - not just
those exactly inside the page object.

This is why I thought...

<xsl:template match="navigation/item"/>
<xsl:template match="menu/item"/>

..should do it

(Hope this is not too off topic)
--
Torsten

---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-users-help@xml.apache.org


RE: xsl matching

Posted by Torsten Curdt <tc...@dff.st>.
> > just a simple question...
> > ....don't know if got it right from book.
> > Doesn't seem to work in real life.
> > 
> > I have a XML file:
> > <page>
> > <navigation>
> > <item/>
> > <item/>
> > </navigation>
> > <menu>
> > <item/>
> > <item/>
> > </menu>
> > </page>
> > 
> > Now I have two different items but wanna
> > match them differently. Thought it was:
> > 
> > <xsl:template match="navigation/item">
> > </xsl:template>
> > 
> > <xsl:template match="menu/item">
> > </xsl:template>
> > 
> > ...but it doesn't seem to work.
> what's the context?

Well, if I see it from a OOP point of view:
I have an object navigation and an object menu.
Both have an attribute named item. But since
they are items from different objects (which
is reflected in the hierachic DOM structure)
they are also wanted to be displayed differently.

> Within 
> <xsl:template match="page">
>     <xsl:apply-templates/>
> </xsl:template>
> 
>  <xsl:template match="navigation">
>     <xsl:apply-templates select="item" mode="navigation"/>
>  </xsl:template>
> 
>  <xsl:template match="item" mode="navigation">
>    ...
>  </xsl:template>
> 
> 
> you can use mode="navigation", mode="menu" if you want different 
> templates:

This will works... but actually I think it's a bit
of a work around.
Shouldn't I be able to match it like I can match
top level objects via:

<xsl:template match="/toplevelobject">

This is a match that reflects the hierachic position
of the object. If not relative... shouldn't it work
absolute like this ?

<xsl:template match="/page/navigation/item"/>

<xsl:template match="/page/menu/item"/>

But actually I'd like to have it to match
for any menu or navigation item - not just
those exactly inside the page object.

This is why I thought...

<xsl:template match="navigation/item"/>
<xsl:template match="menu/item"/>

..should do it

(Hope this is not too off topic)
--
Torsten

Re: xsl matching

Posted by Mark Washeim <31...@t-online.de>.
on 19/7/00 11:12 am, Torsten Curdt at tcurdt@dff.st wrote:

> just a simple question...
> ....don't know if got it right from book.
> Doesn't seem to work in real life.
> 
> I have a XML file:
> 
> <page>
> <navigation>
> <item/>
> <item/>
> </navigation>
> <menu>
> <item/>
> <item/>
> </menu>
> </page>
> 
> Now I have two different items but wanna
> match them differently. Thought it was:
> 
> <xsl:template match="navigation/item">
> ..
> </xsl:template>
> 
> <xsl:template match="menu/item">
> ..
> </xsl:template>
> 
> ...but it doesn't seem to work.
> --
> Torsten
> 
> 

what's the context?

Within 
<xsl:template match="page">
    <xsl:apply-templates/>
</xsl:template>

 <xsl:template match="navigation">
    <xsl:apply-templates select="item" mode="navigation"/>
 </xsl:template>

 <xsl:template match="item" mode="navigation">
   ...
 </xsl:template>


you can use mode="navigation", mode="menu" if you want different templates:

<xsl:template match="page">
    <xsl:apply-templates/>
</xsl:template> 



-- 
Mark (Poetaster) Washeim

'On the linen wrappings of certain mummified remains
found near the Etrurian coast are invaluable writings
that await translation.

Quem colorem habet sapientia?'

Evan S. Connell