This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Conditional text using attributes -- saxon:evaluate?


| Your augmented identity transformation is just what I had in mind --
| conditional inclusion based only on attribute names/values.
| 
| To further parameterize it, would it be possible to also specify an
| attribute name as a parameter along with the value
|
| For example, it would be very nice to be able to do something like:
| 
|   saxon test.xml beth.xsl attribute='os' value='PC'

This should do it:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="attribute">os</xsl:param>
  <xsl:param name="value">Mac</xsl:param>
  <xsl:template match="node()|@*">
    <xsl:choose>
      <!-- 
       | If current node is an element having an attribute whose
       | local name (i.e. disregarding namespace prefix) matches
       | $attribute and whose value is not equals to $value, do nothing.
       +-->
      <xsl:when test="self::*[@*[local-name()=$attribute and . !=$value]]"/>
      <!-- 
       | Otherwise, copy it and process it's attributes/child nodes
       +-->
      <xsl:otherwise>
        <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

This may be less well-performing that the previous one
with a hard-coded attribute name, but is more generic...

If I alter my source to use the 'role' attribute like this:

<?xml version="1.0"?>
<article>
  <articleinfo>
    <title role="PC">Starting the timer using Microsoft Windows</title>
    <title role="Mac">Starting the timer using MacOS</title>
  </articleinfo>
  <para>
    Press the <emphasis role="PC">Left</emphasis> mouse button to start 
    the timer. You can use the <emphasis role="Mac">Finder</emphasis>
    <emphasis role="PC">Start Button</emphasis> to launch a new program.
  </para>
</article>

Then I can now do:

$ saxon test3.xml cond2.xsl attribute=role value=PC

<?xml version="1.0" encoding="utf-8"?>
<article>
    <articleinfo>
      <title role="PC">Starting the timer using Microsoft Windows</title>

    </articleinfo>
    <para>
      Press the <emphasis role="PC">Left</emphasis> mouse button to start
      the timer. You can use the
      <emphasis role="PC">Start Button</emphasis> to launch a new program.
    </para>
  </article>

______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]