B
Bernd.Moos
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">
<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
<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">
<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