473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Dec 13 '06 #1
4 1573

pa**********@orient.su.se 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

Dec 13 '06 #2
p.*****@ctncorp.com 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

Dec 13 '06 #3

pa**********@orient.su.se wrote:
p.*****@ctncorp.com 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

Dec 13 '06 #4
Thanks a lot, Pavel, for these clarifications.

/Patrik

p.*****@ctncorp.com wrote:
pa**********@orient.su.se wrote:
p.*****@ctncorp.com 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
Dec 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ladvánszky Károly | last post by:
What is the correct way to match/search a backslash with regular expressions? print re.match('\\m', '\\m').group(0) raises an error while print re.search('\\m', '\\m').group(0) yields 'm' ...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
17
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
5
by: jens | last post by:
I was wondering if anyone could tell be whether it's possible to instruct preg_match not to save subexpressions matches under 0, 1...n keys when using named subexpressions; I.e instruct preg_named...
5
by: Jay S | last post by:
Hi, Can't figure out the following: <concept> I want to match a specified string followed by any string that is *not* a different specified string. </concept> <specific example>
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
1
by: solarin | last post by:
Hi, I've developed a program under VS 6.0. I can compile it and run it, but when I try to debbug , all my breakpoints are dissabled and I can see the following messages: Loaded...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.