Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 13th, 2006, 08:15 AM
patrik.nyman@orient.su.se
Guest
 
Posts: n/a
Default Matching twice

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

  #2  
Old December 13th, 2006, 09:35 AM
p.lepin@ctncorp.com
Guest
 
Posts: n/a
Default Re: Matching twice


patrik.nyman@orient.su.se wrote:
Quote:
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

  #3  
Old December 13th, 2006, 10:05 AM
patrik.nyman@orient.su.se
Guest
 
Posts: n/a
Default Re: Matching twice

p.lepin@ctncorp.com wrote:
Quote:
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.
Quote:
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?
Quote:
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

  #4  
Old December 13th, 2006, 11:45 AM
p.lepin@ctncorp.com
Guest
 
Posts: n/a
Default Re: Matching twice


patrik.nyman@orient.su.se wrote:
Quote:
p.lepin@ctncorp.com wrote:
Quote:
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.
Quote:
</xsl:template>
>
It seems to work fine with your code.
As it should.
Quote:
Quote:
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.
Quote:
Quote:
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.
Quote:
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

  #5  
Old December 13th, 2006, 01:45 PM
patrik.nyman@orient.su.se
Guest
 
Posts: n/a
Default Re: Matching twice

Thanks a lot, Pavel, for these clarifications.

/Patrik

p.lepin@ctncorp.com wrote:
Quote:
patrik.nyman@orient.su.se wrote:
Quote:
p.lepin@ctncorp.com wrote:
Quote:
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.
>
Quote:
</xsl:template>

It seems to work fine with your code.
>
As it should.
>
Quote:
Quote:
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.
>
Quote:
Quote:
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.
>
Quote:
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
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles