Removing duplicate text nodes

W

wooks

I would appreciate some guidance not just to the solution but why my
own solutions don't seem to work. Copy.xsl in the code below is an
imported identity template.

Non solution 1
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

Non Solution 2 (because I suspected there may be some issues about
what is the current node)

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="text()">
<xsl:apply-templates select="text()[not(.=following::text())]"/>
</xsl:template>
</xsl:stylesheet
 
B

Ben Edgington

I would appreciate some guidance not just to the solution but why my
own solutions don't seem to work. Copy.xsl in the code below is an
imported identity template.

Non solution 1
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

Non Solution 2 (because I suspected there may be some issues about
what is the current node)

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="text()">
<xsl:apply-templates select="text()[not(.=following::text())]"/>
</xsl:template>
</xsl:stylesheet

Perhaps you could show an example of what you hope to achieve.
Your first transformation seems to do exactly what you ask:
it removes a duplicated text node.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

with
<foo>
<a>aaa</a>
<a>bbb</a>
<a>aaa</a>
</foo>

gives
<foo><a/><a>bbb</a>
<a>aaa</a>
</foo>

Ie. the first "aaa" text node is removed. Or were you
after something else?

Ben
 
W

wooks

Ben Edgington said:
I would appreciate some guidance not just to the solution but why my
own solutions don't seem to work. Copy.xsl in the code below is an
imported identity template.

Non solution 1
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

Non Solution 2 (because I suspected there may be some issues about
what is the current node)

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="text()">
<xsl:apply-templates select="text()[not(.=following::text())]"/>
</xsl:template>
</xsl:stylesheet

Perhaps you could show an example of what you hope to achieve.
Your first transformation seems to do exactly what you ask:
it removes a duplicated text node.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

with
<foo>
<a>aaa</a>
<a>bbb</a>
<a>aaa</a>
</foo>

gives
<foo><a/><a>bbb</a>
<a>aaa</a>
</foo>

Ie. the first "aaa" text node is removed. Or were you
after something else?

Ben

No that is what I want and it does work.
There must have been something in the XML I was using. Will
investigate further. Thanks.
 
W

wooks

Ben Edgington said:
I would appreciate some guidance not just to the solution but why my
own solutions don't seem to work. Copy.xsl in the code below is an
imported identity template.

Non solution 1
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

Non Solution 2 (because I suspected there may be some issues about
what is the current node)

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="copy.xsl"/>
<xsl:template match="text()">
<xsl:apply-templates select="text()[not(.=following::text())]"/>
</xsl:template>
</xsl:stylesheet

Perhaps you could show an example of what you hope to achieve.
Your first transformation seems to do exactly what you ask:
it removes a duplicated text node.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()[.=following::text()]"/>
</xsl:stylesheet>

with
<foo>
<a>aaa</a>
<a>bbb</a>
<a>aaa</a>
</foo>

gives
<foo><a/><a>bbb</a>
<a>aaa</a>
</foo>

Ie. the first "aaa" text node is removed. Or were you
after something else?

Ben

Realisation has just dawned... the xml was not well formed (was
generated from a program but had no parent node).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top