472,328 Members | 1,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How to find and replace sequences of elements

Given the following XML document:

<text>
<p>
<w>Ronaldo</w>
<w>scoredw>
<w>the</w>
<w>1</w>
<c>:</c>
<w>1</w>
<w>opener</w>
</p>
...
<text>

I need to

1) find patterns like <w>...</w><c>:</c><w>...</w>, i.e. any
'w'-element, followed by a 'c'-element with text ':', followed by any
'w'-element
2) replace this pattern with <w>...:...</w>, i.e. a single element in
which the text of the found elements is aligned

Right now, I am doing this with the following stylesheet:

-----------------------------------------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<!-- go through the whole document, copy everything -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<!-- for c-elements whose text is ':'... -->
<xsl:template match="//c[text()=':']">
<xsl:if test="name(preceding-sibling::*[1])='w' and
name(following-sibling::*[1])='w'">
<!-- ... make a new element and put the text of the matching
pattern inside -->
<w type='score'>
<xsl:value-of select="preceding-sibling::*[1]/text()"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="following-sibling::*[1]/text()"/>
</w>
</xsl:if>
</xsl:template>

<!-- make sure not to copy w-elements that have been taken care of by
the former template -->
<xsl:template match="//w[name(following-sibling::*[1])='c' and
following-sibling::*[1]/text()=':' and
name(following-sibling::*[2])='w']">
</xsl:template>
<xsl:template match="//w[name(preceding-sibling::*[1])='c' and
preceding-sibling::*[1]/text()=':' and
name(preceding-sibling::*[2])='w']">
</xsl:template>
</xsl:stylesheet>
-----------------------------------------------------------

This works OK but it looks so awkward! What's more: if the patterns I
want to replace get longer, it becomes increasingly difficult to take
care of all the things that must not be copied a second time (i.e. the
last to templates in the above). Can anybody point me to a more elegant
way of doing this?

Thanks very much,

Thomas

Sep 6 '05 #1
9 1847
Be********@gmail.com <Be********@gmail.com> wrote:
Given the following XML document:

<text>
<p>
<w>Ronaldo</w>
<w>scoredw>
<w>the</w>
<w>1</w>
<c>:</c>
<w>1</w>
<w>opener</w>
</p>
...
<text>

I need to

1) find patterns like <w>...</w><c>:</c><w>...</w>, i.e. any
'w'-element, followed by a 'c'-element with text ':', followed by any
'w'-element
2) replace this pattern with <w>...:...</w>, i.e. a single element in
which the text of the found elements is aligned

Right now, I am doing this with the following stylesheet:

-----------------------------------------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<!-- go through the whole document, copy everything -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<!-- for c-elements whose text is ':'... -->
<xsl:template match="//c[text()=':']">
<xsl:if test="name(preceding-sibling::*[1])='w' and
name(following-sibling::*[1])='w'">
<!-- ... make a new element and put the text of the matching
pattern inside -->
<w type='score'>
<xsl:value-of select="preceding-sibling::*[1]/text()"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="following-sibling::*[1]/text()"/>
</w>
</xsl:if>
</xsl:template>

<!-- make sure not to copy w-elements that have been taken care of by
the former template -->
<xsl:template match="//w[name(following-sibling::*[1])='c' and
following-sibling::*[1]/text()=':' and
name(following-sibling::*[2])='w']">
</xsl:template>
<xsl:template match="//w[name(preceding-sibling::*[1])='c' and
preceding-sibling::*[1]/text()=':' and
name(preceding-sibling::*[2])='w']">
</xsl:template>
</xsl:stylesheet>
-----------------------------------------------------------

This works OK but it looks so awkward! What's more: if the patterns I
want to replace get longer, it becomes increasingly difficult to take
care of all the things that must not be copied a second time (i.e. the
last to templates in the above). Can anybody point me to a more elegant
way of doing this?

Thanks very much,

Thomas


Key insight is "split" and then "join". In extended Bash shell,
a=`< file.xml`
set -- "${a|-</w>[[:space:]]*<c>:</c>[[:space:]]*<w>}"
echo "${*|,:}"
where
${var|-regex} splits the string on 'regex'
${list|,sep} joins the elements using 'sep' string.

You can do the same thing in Python also.

--
William Park <op**********@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Sep 6 '05 #2

<Be********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Given the following XML document:

<text>
<p>
<w>Ronaldo</w>
<w>scoredw>
<w>the</w>
<w>1</w>
<c>:</c>
<w>1</w>
<w>opener</w>
</p>
...
<text>

I need to

1) find patterns like <w>...</w><c>:</c><w>...</w>, i.e. any
'w'-element, followed by a 'c'-element with text ':', followed by any
'w'-element
2) replace this pattern with <w>...:...</w>, i.e. a single element in
which the text of the found elements is aligned

Right now, I am doing this with the following stylesheet:

-----------------------------------------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<!-- go through the whole document, copy everything -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<!-- for c-elements whose text is ':'... -->
<xsl:template match="//c[text()=':']">
<xsl:if test="name(preceding-sibling::*[1])='w' and
name(following-sibling::*[1])='w'">
<!-- ... make a new element and put the text of the matching
pattern inside -->
<w type='score'>
<xsl:value-of select="preceding-sibling::*[1]/text()"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="following-sibling::*[1]/text()"/>
</w>
</xsl:if>
</xsl:template>

<!-- make sure not to copy w-elements that have been taken care of by
the former template -->
<xsl:template match="//w[name(following-sibling::*[1])='c' and
following-sibling::*[1]/text()=':' and
name(following-sibling::*[2])='w']">
</xsl:template>
<xsl:template match="//w[name(preceding-sibling::*[1])='c' and
preceding-sibling::*[1]/text()=':' and
name(preceding-sibling::*[2])='w']">
</xsl:template>
</xsl:stylesheet>
-----------------------------------------------------------

This works OK but it looks so awkward! What's more: if the patterns I
want to replace get longer, it becomes increasingly difficult to take
care of all the things that must not be copied a second time (i.e. the
last to templates in the above). Can anybody point me to a more elegant
way of doing this?

Use the "Tree Visitor" pattern like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()[1]"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>

<xsl:template match="w[following-sibling::*[1][self::c]
and
following-sibling::c[1]=':'
and
following-sibling::*[2][self::w]
]">
<w>
<xsl:value-of select=
"concat(.,following-sibling::*[1],following-sibling::*[2])"/>
</w>
<xsl:apply-templates select=
"following-sibling::*[2]/following-sibling::node()[1]"/>
</xsl:template>

</xsl:stylesheet>

When this transformation is applied on your source xml (corrected to be at
least well-formed):

<text>
<p>
<w>Ronaldo</w>
<w>scoredw</w>
<w>the</w>
<w>1</w>
<c>:</c>
<w>1</w>
<w>opener</w>
</p>
...
</text>

the wanted result is produced:

<text>
<p>
<w>Ronaldo</w>
<w>scoredw</w>
<w>the</w>
<w>1:1</w>
<w>opener</w>
</p>
...
</text>

Here I use a variation of the identity rule that only applies templates to
one node at a time. This results in achieving maximum flexibility in
processing the nodes of the xml document really sequentially.
Hope this helped.

Cheers,
Dimitre Novatchev
Sep 6 '05 #3
Thanks, Dimitre, this is very, very helpful!
Just one more question: if the sequence pattern I want to replace
consists of, (e.g.) five elements (and not three like in the example),
I would
1) change the match-value of the last template accordingly and
2) change the arguments of the concat function accordingly and
3) what else?

(I do not quite understand what these statements in your code do:

<xsl:apply-templates select="following-sibling::node()[1]"/> and
<xsl:apply-templates
select="following-sibling::*[2]/following-sibling::node()[1]"/>)

Thanks again...

Sep 7 '05 #4

<Be********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks, Dimitre, this is very, very helpful!
Just one more question: if the sequence pattern I want to replace
consists of, (e.g.) five elements (and not three like in the example),
I would
1) change the match-value of the last template accordingly and
2) change the arguments of the concat function accordingly and
3) what else?
Change:
<xsl:apply-templates
select="following-sibling::*[2]/following-sibling::node()[1]"/>

to

<xsl:apply-templates
select="following-sibling::*[4]/following-sibling::node()[1]"/>


(I do not quite understand what these statements in your code do:

<xsl:apply-templates select="following-sibling::node()[1]"/>

Apply the templates but not to all children of the current node -- just to
its immediate following sibling
and
<xsl:apply-templates
select="following-sibling::*[2]/following-sibling::node()[1]"/>)

Continue to apply templates sequentially, but skipping the three elements
that we processed in the current template, therefore restarting at the
immediate following sibling of the last element we processed in the current
template.
Cheers,
Dimitre Novatchev.
Sep 7 '05 #5
Hi again,

I've been working succesfully with this "Tree Visiting pattern" for a
week now. Yesterday, however, I added Stylesheet assignments, i.e.
things like

<?xml-stylesheet href="file:../../../2HTML.xsl" type="text/xsl"?>

to my XML documents.

When I now apply the pattern, it does what it is supposed to do, but,
on top of this, duplicates the entire document (leading to
non-well-formed XML because there are two root elements). What's even
stranger: when I add a comment before the root element on top of the
XSL assignment, the output triples the input. I know how to avoid this
(take out the XSL assignments etc.), but I'd like to understand it.
What on earth is going on here?

Kind regards,

Thomas

Sep 19 '05 #6

"Be********@gmail.com" <Be********@googlemail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi again,

I've been working succesfully with this "Tree Visiting pattern" for a
week now. Yesterday, however, I added Stylesheet assignments, i.e.
things like

<?xml-stylesheet href="file:../../../2HTML.xsl" type="text/xsl"?>

to my XML documents.

When I now apply the pattern, it does what it is supposed to do, but,
on top of this, duplicates the entire document (leading to
non-well-formed XML because there are two root elements). What's even
stranger: when I add a comment before the root element on top of the
XSL assignment, the output triples the input. I know how to avoid this
(take out the XSL assignments etc.), but I'd like to understand it.
What on earth is going on here?

As you haven't provided any code, the reason is probably the bad weather...
:o)

Cheers,
Dimitre Novatchev.
Sep 20 '05 #7
> As you haven't provided any code, the reason is probably the bad weather...
:o)

Cheers,
Dimitre Novatchev.


The weather actually couldn't have been any better ;-) The strange
things happen with *every* variant of the TreeVisitor pattern, for
instance:

1) XSL-File

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()[1]"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
</xsl:stylesheet>

2) XML input

<?xml version="1.0" encoding="UTF-8"?>
<!-- any old comment -->
<x>
<y>
<w>le</w>
</y>
</x>

3) XML output

<?xml version="1.0" encoding="UTF-8"?><!-- any old comment --><x>
<y>
<w>le</w>
</y>
</x><x>
<y>
<w>le</w>
</y>
</x>

In this example, I did the transformation with whatever is built into
the EditiX editor. But the same phenomenon occurs when I use JDOM to do
my transformations.

Thanks for your help,

Thomas

Sep 20 '05 #8
Yes. there was a subtle bug in my code.

The solution is to replace:

<xsl:template match="@* | node()">

with:

<xsl:template match="/ | @* | node()">
What's happening?

Someone (like me) would think that the node test:

node()

matches the root node (document node in XPath 2.0 lingo).

And yes, this *is* true in XPath.

And no, this *isn't true* for a match pattern.

Because a match pattern matches something when the match pattern is
evaluated from its parent, then

node()

will not match the document node "/", because the document node has no
parent by definition.

Because the document node is not matched by any template in our code, the
default template rule is used:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>

The xsl:apply-templates instruction causes templates to be applied to *both*
children of the document node (the comment node and the element node), which
produces two almost identical sequences (one including the comment, the
other not) and thus we have every node repeated in the output, with the
exception of the comment node.
Cheers,
Dimitre Novatchev.
Cheers,
Dimitre Novatchev.


"Be********@gmail.com" <Be********@googlemail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
As you haven't provided any code, the reason is probably the bad
weather...
:o)

Cheers,
Dimitre Novatchev.


The weather actually couldn't have been any better ;-) The strange
things happen with *every* variant of the TreeVisitor pattern, for
instance:

1) XSL-File

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()[1]"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
</xsl:stylesheet>

2) XML input

<?xml version="1.0" encoding="UTF-8"?>
<!-- any old comment -->
<x>
<y>
<w>le</w>
</y>
</x>

3) XML output

<?xml version="1.0" encoding="UTF-8"?><!-- any old comment --><x>
<y>
<w>le</w>
</y>
</x><x>
<y>
<w>le</w>
</y>
</x>

In this example, I did the transformation with whatever is built into
the EditiX editor. But the same phenomenon occurs when I use JDOM to do
my transformations.

Thanks for your help,

Thomas

Sep 21 '05 #9
Got it, changed it, it did the trick.

Thank you so much,

Thomas

Sep 21 '05 #10

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

Similar topics

5
by: Minho Chae | last post by:
Hello, python lovers!! I'm trying to create combinations of sequences. For example, if the sequence is 'acgt' and the length is 8, then I...
5
by: mp | last post by:
when I write this in c# : strFileToLoad = strFileToLoad.Substring(0, 2) + strFileToLoad.Substring(2).Replace("\\", "\"); it doesnt like the last...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc'...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as...
8
by: Guy | last post by:
Hi, I'm trying to run this code : strFileContentsHTML.Replace(vbLf, "<br>") strFileContentsHTML.Replace(vbCrLf, "<br>")...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to...
15
by: =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post by:
I have a text file that contains about 8 to 10 text sequences that I need to replace. I want to search and replace all 8 to 10 text sequence...
3
by: bstjean | last post by:
Hi everyone, I am trying to find an efficient way to perform a special query. Let me explain what I want. Let's say we are looking for all...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.