Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Matching twice

Reply
Thread Tools

Matching twice

 
 
patrik.nyman@orient.su.se
Guest
Posts: n/a
 
      12-13-2006
Hi!

I have elements like these:

<name style="it">SomeData</name>
<otherName style="it">SomeOtherData</otherName>

and tries to transform these with the following:

<xsl:template match="*[@style='it']">
<i><xsl:apply-templates/></i>
</xsl:template>

<xsl:template match="name">
... do something ...
</xsl:template>

<xsl:template match="otherName">
... do something ...
</xsl:template>

But as you might guess, it does not work, because only
the first template matches. That is, after the first
template there is

<i>SomeData</i>

but I would like to have

<name><i>SomeData</i></name>

Could someone please tell me how?

/Patrik

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      12-13-2006

wrote:
> I have elements like these:
>
> <name style="it">SomeData</name>
> <otherName style="it">SomeOtherData</otherName>
>
> and tries to transform these with the following:
>
> <xsl:template match="*[@style='it']">
> <i><xsl:apply-templates/></i>
> </xsl:template>
>
> <xsl:template match="name">
> ... do something ...
> </xsl:template>
>
> <xsl:template match="otherName">
> ... do something ...
> </xsl:template>


You really should define 'do something', because the
solutions possible depend on what you're trying to do with
the elements.

For example:

<xsl:template match="text()[../@style='it']">
<i><xsl:copy/></i>
</xsl:template>

This template will process all the text nodes that are
children of elements with style attribute being equal to
'it', BUT, it will only work if you're not trying to do
anything with text nodes when you match the corresponding
elements. And, of course, you'll have to add
<xsl:apply-templates select="text()"/> or somesuch
somewhere (or simply use the identity transformation).

--
Pavel Lepin

 
Reply With Quote
 
 
 
 
patrik.nyman@orient.su.se
Guest
Posts: n/a
 
      12-13-2006
wrote:

> You really should define 'do something', because the
> solutions possible depend on what you're trying to do with
> the elements.
>
> For example:
>
> <xsl:template match="text()[../@style='it']">
> <i><xsl:copy/></i>
> </xsl:template>
>
> This template will process all the text nodes that are
> children of elements with style attribute being equal to
> 'it',


This is an abbrevated verion of a template:

<xsl:template match="name">
<xsl:variable name="info" select="@flag"/>
<xsl:variable name="link" select="@url"/>
<a title="{$info}" href="{$link}"
target="_blank"><xsl:apply-templates/></a>
</xsl:template>

It seems to work fine with your code.

> BUT, it will only work if you're not trying to do
> anything with text nodes when you match the corresponding
> elements.


This I cannot follow, could you please give an example?

> And, of course, you'll have to add
> <xsl:apply-templates select="text()"/> or somesuch
> somewhere (or simply use the identity transformation).


The simple <xsl:apply-templates/> seems to work in my case.
Are there any possible drawbacks, or specific situations where
<xsl:apply-templates select="text()"/> should rather be used.

Thanks a lot for your help, Pavel!

/Patrik

 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      12-13-2006

wrote:
> wrote:
> > You really should define 'do something', because the
> > solutions possible depend on what you're trying to do
> > with the elements.
> >
> > For example:
> >
> > <xsl:template match="text()[../@style='it']">
> > <i><xsl:copy/></i>
> > </xsl:template>
> >
> > This template will process all the text nodes that are
> > children of elements with style attribute being equal
> > to 'it',

>
> This is an abbrevated verion of a template:
>
> <xsl:template match="name">
> <xsl:variable name="info" select="@flag"/>
> <xsl:variable name="link" select="@url"/>
> <a title="{$info}" href="{$link}"
> target="_blank"><xsl:apply-templates/></a>


Oh, great, that's how it really should be done.

> </xsl:template>
>
> It seems to work fine with your code.


As it should.

> > BUT, it will only work if you're not trying to do
> > anything with text nodes when you match the
> > corresponding elements.

>
> This I cannot follow, could you please give an example?


Well, consider the following example:

<xsl:template match="name">
<xsl:copy>
<xsl:value-of
select=
"
translate
(
.,
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
"/>
</xsl:copy>
</xsl:template>

If you processed your text nodes that way, obviously, my
solution wouldn't work, because templates would never be
applied to them. That's one of the reasons to stick to
template-based processing whenever you can.

> > And, of course, you'll have to add
> > <xsl:apply-templates select="text()"/> or somesuch
> > somewhere (or simply use the identity transformation).

>
> The simple <xsl:apply-templates/> seems to work in my
> case.


If select attribute is omitted, templates are applied to
all child::node()s, which includes all the text node
children.

> Are there any possible drawbacks, or specific situations
> where <xsl:apply-templates select="text()"/> should
> rather be used.


If you need to process text nodes only, you should use
text(). If you need to process all the children nodes,
<xsl:apply-templates/> should be used. In my example, I
used <xsl:apply-templates select="text()"/> for clarity's
sake.

--
Pavel Lepin

 
Reply With Quote
 
patrik.nyman@orient.su.se
Guest
Posts: n/a
 
      12-13-2006
Thanks a lot, Pavel, for these clarifications.

/Patrik

wrote:
> wrote:
> > wrote:
> > > You really should define 'do something', because the
> > > solutions possible depend on what you're trying to do
> > > with the elements.
> > >
> > > For example:
> > >
> > > <xsl:template match="text()[../@style='it']">
> > > <i><xsl:copy/></i>
> > > </xsl:template>
> > >
> > > This template will process all the text nodes that are
> > > children of elements with style attribute being equal
> > > to 'it',

> >
> > This is an abbrevated verion of a template:
> >
> > <xsl:template match="name">
> > <xsl:variable name="info" select="@flag"/>
> > <xsl:variable name="link" select="@url"/>
> > <a title="{$info}" href="{$link}"
> > target="_blank"><xsl:apply-templates/></a>

>
> Oh, great, that's how it really should be done.
>
> > </xsl:template>
> >
> > It seems to work fine with your code.

>
> As it should.
>
> > > BUT, it will only work if you're not trying to do
> > > anything with text nodes when you match the
> > > corresponding elements.

> >
> > This I cannot follow, could you please give an example?

>
> Well, consider the following example:
>
> <xsl:template match="name">
> <xsl:copy>
> <xsl:value-of
> select=
> "
> translate
> (
> .,
> 'abcdefghijklmnopqrstuvwxyz',
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
> )
> "/>
> </xsl:copy>
> </xsl:template>
>
> If you processed your text nodes that way, obviously, my
> solution wouldn't work, because templates would never be
> applied to them. That's one of the reasons to stick to
> template-based processing whenever you can.
>
> > > And, of course, you'll have to add
> > > <xsl:apply-templates select="text()"/> or somesuch
> > > somewhere (or simply use the identity transformation).

> >
> > The simple <xsl:apply-templates/> seems to work in my
> > case.

>
> If select attribute is omitted, templates are applied to
> all child::node()s, which includes all the text node
> children.
>
> > Are there any possible drawbacks, or specific situations
> > where <xsl:apply-templates select="text()"/> should
> > rather be used.

>
> If you need to process text nodes only, you should use
> text(). If you need to process all the children nodes,
> <xsl:apply-templates/> should be used. In my example, I
> used <xsl:apply-templates select="text()"/> for clarity's
> sake.
>
> --
> Pavel Lepin


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Matching twice patrik.nyman@orient.su.se XML 0 12-13-2006 08:17 AM
[rake] rule with regex executed twice for same matching string Joel VanderWerf Ruby 1 11-10-2006 08:33 PM
twice(twice(x)) Kiuhnm C++ 2 04-01-2006 04:41 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57