Re: [xsl] Get value from external file

Subject: Re: [xsl] Get value from external file
From: Mike Brown <mike@xxxxxxxx>
Date: Wed, 26 Feb 2003 06:03:52 -0700 (MST)
Anders Svensson wrote:
> if I understand you correctly. As I see the example you wrote, it is a way 
> to process the element differently depending on the profile. But I do not 
> want to process the element differently at any time, all I want to do is to 
> "filter out" all <ELEMENT> that don't have the attribute "Help='Yes'" (for 
> instance). So I really don't want to have a set of four or five templates 
> for the same element (one for each mode) when they are exactly the same...

You still have a condition, somehow expressed in this external file, that
indicates either specifically what you want to ignore in the source tree, or
just the fact that you want to ignore something, correct? But the ignorable 
elements may be scattered throughout the source tree, right?

I suggest that you "filter out" not necessarily by trying to avoid selecting
certain nodes for processing, but by walking the tree as normal (e.g. with the
built-in templates), and each time you encounter an element, you decide
whether or not it is ignorable. Since variable references aren't allowed in
match patterns, you have to perform the test inside each template.

For example (untested):

<ignore>
  <element name="ELEMENT">
    <attr name="Help" value="Yes"/>
  </element>
</ignore>

Then your stylesheet could be something like...

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

<xsl:variable name="elementsToIgnore" select="document('ignore.xml')/ignore/element"/>

<!--
  this template
  produces a result tree fragment containing some string 
  data if the given element is ignorable
-->
<xsl:template name="checkElement">
  <xsl:param name="thisElement" select="."/>
  <xsl:variable name="ignorableElemsToTest" select="$elementsToIgnore[@name=$n]"/>
  <xsl:for-each select="$ignorableElemsToTest">
    <xsl:for-each select="attr">
      <xsl:if test="$thisElement/@*[name()=current()/@name]=current()/@value">1</xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

<xsl:template match="ELEMENT">
  <xsl:variable name="thisElementIsIgnorable">
    <xsl:call-template name="checkElement"/>
  </xsl:variable>
  <xsl:if test="not(string($thisElementIsIgnorable))">
    <!-- this node is not ignorable today, so go ahead and generate some output -->
    <p>help text: <xsl:value-of select="."/></p>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

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


Current Thread