Re: [xsl] xsl:value-of select : how to make some tags printed and others not

Subject: Re: [xsl] xsl:value-of select : how to make some tags printed and others not
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 07 Oct 2003 11:54:55 -0400
Mukul,

Since David is probably working and since I already stuck my nose in, I'll do so again. (He will of course correct me where I go wrong.)

At 11:10 AM 10/7/2003, you wrote:
Hi Judith,
  Taking hint from David Carlisle's answer, I have
written this XSL --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

   <xsl:template match="/">
     <xsl:apply-templates select="//text" />
   </xsl:template>

This template reaches down from the root and directly selects all <text> element descendants. Any intermediate node structures (any wrapper elements the <text> elements appear inside of) will not be processed.


  <xsl:template match="//text">
     <xsl:copy>
       <xsl:apply-templates select="node()" />
     </xsl:copy>
   </xsl:template>

Apart from a different priority (0.5 instead of 0, due to the match pattern), this is the same as


<xsl:template match="text">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

which is, in effect, the identity template. (Except any attributes on the <text> element will be skipped.)

  <xsl:template match="sw">
     <xsl:value-of select="." />
   </xsl:template>

This is what David suggested, but not what he meant to suggest. It copies the string value of the <sw> element to the result. Note any subordinate elements inside the <sw> will not be processed. (Not what the OP wanted.)


  <xsl:template match="sub">
     <sub>
       <xsl:value-of select="." />
     </sub>
   </xsl:template>

This matches the <sub> in the source, and creates a <sub> in the output, but:


1. Any element nodes inside the <sub> (such as <i> or whatever) will not be processed: the value-of simply copies a string.
2. This template will never get hit in any case, since the rest of the stylesheet provides no way a <sub> element will ever be selected and matched.


The solution David thought he was suggesting is:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="sw">
  <xsl:apply-templates/>
</xsl:template>

which works as follows:

1. The first template, with a priority of -0.5, matches any node, copying it to the result, and processing its attributes and children, placing their results inside the copied node.

2. The second template, with a priority of 0 (beating out the first template), matches any <sw> node. It does not create any output, but it does process the child nodes of <sw>, ensuring they get processed. So <sub> elements inside will be matched by the first template, and copied. (So will their children and descendants.)

For the benefit of those just learning XPath (and XSLT pattern-matching rules), the above is the same as:

<xsl:template match="attribute::*|child::node()" priority="-0.5">
  <!-- known in XSLT-speak as the "identity template" since it creates a
       node-for-node copy of the input tree -->
  <xsl:copy>
    <xsl:apply-templates select="attribute::*|child::node("/>
  </xsl:copy>
</xsl:template>

<xsl:template match="sw" priority="0">
  <xsl:apply-templates select="child::node()"/>
</xsl:template>

I hope this clarifies --

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread