Thanks Chris, I edited your solution according to my requirements, so I would like to share this problem with everyone in the future.
Note. . This moves the text inside the anchor and removes the text outside. Fixes anchors that should contain only text, not html. those. My solution fixes this tag:
<p><a name="anchor1" id="anchor1"></a>Anchor text</p>
For
<p><a name="anchor1" id="anchor1">Anchor text</a></p>
But not this:
<p><a name="anchor1" id="anchor1"></a><h1>Anchor text</h1></p>
Here is my xsl. Hope this helps you get the database, I'm sure you can easily update it to find the next tag (I don't require this for my solution).
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" method="html" cdata-section-elements="script"/> <xsl:template match="/ | node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <xsl:template match="a[(@id) and (count(node()) = 0)]"> <xsl:copy> <xsl:for-each select="@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:value-of select="normalize-space(following-sibling::text())"/> </xsl:copy> </xsl:template> <xsl:template match="text()[preceding-sibling::a[(@id) and (count(node()) = 0)]]" ></xsl:template> </xsl:stylesheet>
Here is my test XML
<?xml version ="1.0"?> <?xml-stylesheet type="text/xsl" href="tridionhtmlfield.xsl"?> <html> <head></head> <body> <p><a id="anchorlink" name="anchorlink" title="Anchor link" href="#Anchor">Anchor link</a>Some text after</p> <p><a name="broken-with-html-name" id="broken-with-html-id"></a><h1>Anchor - broken with html</h1></p> <p><a name="broken-text-only-name" id="broken-text-only-id"></a>Anchor - broken text only</p> <p><a name="broken-notext-name" id="broken-notext-id"></a></p> <p><a name="correct-name" id="correct-id">Anchor - correctly rendered</a> Some text after</p> </body> </html>
After conversion:
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> <body> <p><a id="anchorlink" name="anchorlink" title="Anchor link" href="#Anchor">Anchor link</a>Some text after</p> <p><a name="broken-with-html-name" id="broken-with-html-id"></a><h1>Anchor - broken with html</h1></p> <p><a name="broken-text-only-name" id="broken-text-only-id">Anchor - broken text only</a></p> <p><a name="broken-notext-name" id="broken-notext-id"></a></p> <p><a name="correct-name" id="correct-id">Anchor - correctly rendered</a> Some text after</p> </body> </html>
Hope this helps
source share