I have problem with parsing XML file using XLTS.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">
<body style="margin-top: 0px;">
<a name="top"/>
<a name="menu"> </a>
<a href="cool html"> </a>
<table width="100%" cellspacing="0" cellpadding="2" border="0" class="aws_border sortable"/>
</body>
</html>
And I need to delete all node with <a name="something"> </a>, while preserving <a href> nodes and other nodes in document.
I have tried
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="body">
<xsl:for-each select="a">
<xsl:if test="@href != '' ">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But it preserves only <a href > nodes, and deleting all the other nodes.
Preserving all nodes and changing only a few always goes like this:
<xsl:for-each>. You don’t need it.XSLT:
That’s it.
Point 3 is actually pretty important. Avoid
<xsl:for-each>in all XSLT you write. It seems familiar and helpful, but it’s not. Its use tends to result in clunky, monolithic, deeply nested XSLT code that is hard to re-use.Always try to prefer
<xsl:template>and<xsl:apply-templates>over<xsl:for-each>.