Chrissy H
Chrissy H

Reputation: 461

How do I excluding nodes from XLST

Sorry, this is a really novice question. My real problem involves translating HTML to Open Office XML, but this illustrates the issue I am seeing. I want to make sure the "b" node is ignored in my processing, i.e. not get the 123 at the end of the results output.
XML:

<?xml version="1.0"?><?xml-stylesheet type="text/xsl"?>
<a> 
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>
<b>123
</b>
</a>

XSLT:

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="hello-world">
  <HTML><HEAD><TITLE></TITLE></HEAD><BODY><H1>
    <xsl:value-of select="greeting"/>
        </H1>
     <xsl:apply-templates select="greeter"/>
    </BODY></HTML>
</xsl:template>

<xsl:template match="greeter">
  <DIV>from <I><xsl:value-of select="."/></I></DIV>
</xsl:template>
</xsl:stylesheet>

Results:

<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE></TITLE>
   </HEAD>
   <BODY>
      <H1>Hello, World!</H1>
      <DIV>from <I>An XSLT Programmer</I></DIV>
   </BODY>
</HTML>
123

Upvotes: 0

Views: 25

Answers (2)

Sebastien
Sebastien

Reputation: 2714

I see you added an answer, but thought I could add more information.

Your first attempt was good, you could have simply added another template to ignore the "b" nodes.

<xsl:template match="b"/>

What was happening is that the XSLT built-in template rules include by default a template that copies the text of any node that's not explicitely matched by your templates.

Reference: docstore.mik.ua/orelly/xml/xmlnut/ch08_07.htm

See the output of your transformation with the added template: https://xsltfiddle.liberty-development.net/aixRus

Upvotes: 1

Chrissy H
Chrissy H

Reputation: 461

The issue was that I wasn't selecting the whole document to begin with. If I change the xslt as follows, it then works


<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/*">
  <xsl:apply-templates select='hello-world'/>
</xsl:template>

<xsl:template match="hello-world">
  <HTML><HEAD><TITLE></TITLE></HEAD><BODY><H1>
    <xsl:value-of select="greeting"/>
        </H1>
     <xsl:apply-templates select="greeter"/>
    </BODY></HTML>
</xsl:template>

<xsl:template match="greeter">
<DIV>from <I><xsl:value-of select="."/></I></DIV>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions