Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to find and replace sequences of elements

Reply
Thread Tools

How to find and replace sequences of elements

 
 
Bernd.Moos@gmail.com
Guest
Posts: n/a
 
      09-06-2005
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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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

 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      09-06-2005
<> 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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 <>, 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/
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      09-06-2005

<> wrote in message
news: 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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


 
Reply With Quote
 
Bernd.Moos@gmail.com
Guest
Posts: n/a
 
      09-07-2005
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...

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      09-07-2005

<> wrote in message
news: 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.


 
Reply With Quote
 
Bernd.Moos@gmail.com
Guest
Posts: n/a
 
      09-19-2005
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

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      09-20-2005

"" <> wrote in message
news: 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...
)

Cheers,
Dimitre Novatchev.


 
Reply With Quote
 
Bernd.Moos@gmail.com
Guest
Posts: n/a
 
      09-20-2005
> As you haven't provided any code, the reason is probably the bad weather...
> )
>
> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      09-21-2005
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.




"" <> wrote in message
news: oups.com...
>> As you haven't provided any code, the reason is probably the bad
>> weather...
>> )
>>
>> 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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
>



 
Reply With Quote
 
Bernd.Moos@gmail.com
Guest
Posts: n/a
 
      09-21-2005
Got it, changed it, it did the trick.

Thank you so much,

Thomas

 
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
Re: Can't find elements using ElementTree find method Stefan Behnel Python 0 08-31-2010 09:00 AM
How *build* new elements and *replace* elements with xml.dom.minidom? Chris Seberino Python 3 06-12-2009 05:08 AM
vhdl textio and escape sequences Olaf Petzold VHDL 1 11-28-2005 06:22 PM
accessing elements in multi-dimensional sequences Rodrigo Daunaravicius Python 7 05-28-2004 01:47 PM
Escape sequences and printing Kit C Programming 5 09-17-2003 02:10 AM



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