xsl-list
[Top] [All Lists]

RE: multiple attributes

2002-12-13 08:04:26
Hi Dave.
The following stylesheet has two modes, the default checks if the @att
has any of the chars entered, the block mode checks if the @att has any
of the words entered.

Example:
If wanted='ab' then the default mode copies all nodes that has the char
'a' or the char 'b', the 'block' mode only copy the nodes that has the
word 'ab' on the @att attribute.
If wanted='a b' (your example) then the default mode copies all nodes
that has the char 'a' or the char 'b', the 'block' mode only copy the
nodes that has the word 'a' or the word 'b' on the @att attribute.

Hope that this helps you.

Stylesheet:
 <xsl:param name="wanted" select="'a b'"/>
 <xsl:variable name="attrs" select="translate($wanted,' ','')"/> <!--
this is for the default mode -->
 <xsl:variable name="attr2"> <!-- this is for 'block' mode -->
 <xsl:call-template name="break"/>
 </xsl:variable>
 
 <xsl:template match="*">
  <xsl:choose>
   <xsl:when test="not(translate(@att,$attrs,'')=(_at_)att) or not(@att)">
   <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
   </xsl:copy>
   </xsl:when>
  </xsl:choose>
 </xsl:template>
 
 <xsl:template match="*" mode="block">
  <xsl:variable name="curr_node" select="."/>
  <xsl:variable name="attr">
  <xsl:for-each select="msxsl:node-set($attr2)/attr"><!-- you'll need a
node-set function here -->
   <xsl:if test="contains(concat(' ',$curr_node/@att,' '),concat(' ',.,'
'))">
   <xsl:text>1</xsl:text>
   </xsl:if>
  </xsl:for-each>
  </xsl:variable>

  <xsl:if test="string($attr) or not(@att)">
   <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
   </xsl:copy>
  </xsl:if>
 </xsl:template>
 
 <xsl:template name="break">
  <xsl:param name="s" select="$wanted"/>
  <xsl:choose>
   <xsl:when test="contains($s,' ')">
   <attr><xsl:value-of select="substring-before($s,' ')"/></attr>
   <xsl:call-template name="break">
    <xsl:with-param name="s" select="substring-after($s,' ')"/>
   </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
   <attr><xsl:value-of select="$s"/></attr>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
DPawson(_at_)rnib(_dot_)org(_dot_)uk
Sent: sexta-feira, 13 de Dezembro de 2002 13:03
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] multiple attributes


problem.

<xsl:param name="wanted" select="''"/>

<element att="a b c d">
wanted, contains both a and b

</element>

<element>

wanted, has no att attribute

</element>

<element att="x">
not wanted, block it.
</element>

I want to run an identity transform, but only 
select those elements which either have no @att,
or have an att value which is one of those passed into the stylesheet as
a command line param, e.g.

saxon file.xml ss.xsl "wanted= a b"

should only copy through those elements having an att value 
which contains one or both of a, b.

Hope that's clear.

I can't recall seeing a mxn contains string thingy.
I'm getting stuck with a recursion solution.
Any help appreciated.

TIA DaveP.



 <xsl:template match="*[(_at_)targets]">
    <xsl:variable name="inTarget">
    <xsl:call-template name="tgt">
      <xsl:with-param name="wanted" select="$targets"/>
      <xsl:with-param name="this-elements-targets" select="@targets"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$inTarget">
       <!-- Copy through, wanted -->
       <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
       </xsl:copy>
    </xsl:when>


    <xsl:otherwise/>  <!-- block, not wanted -->
  </xsl:choose>
  </xsl:template>



 <xsl:template name="tgt">
    <xsl:param name="wanted" select="''"/>
    <xsl:param name="this-elements-targets" select="''"/>
    <xsl:if test="$debug">
      <xsl:message>
tgt: wanted   <xsl:value-of select="$wanted"/>
this els:   <xsl:value-of select="$this-elements-targets"/>
      </xsl:message>
    </xsl:if>
<xsl:variable name="onetarget"
select="substring-before($this-elements-targets,'+')"/>
      <xsl:if test="contains($wanted, $onetarget)">
        <xsl:value-of select="true"/>
      </xsl:if>

    <xsl:choose>
      <xsl:when test="string-length($this-elements-targets)= 0">
        <xsl:value-of select="false()"/>
      </xsl:when>
      

      <xsl:otherwise>
        <xsl:call-template name="tgt">
          <xsl:with-param name="this-elements-targets"
select="substring-after($this-elements-targets,'+')"/> <!-- presently
uses + character as seperator. perhaps change to space character -->
          <xsl:with-param name="wanted" select="$wanted"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>


  </xsl:template>


Regards DaveP.

**** snip here *****

- 

NOTICE: The information contained in this email and any attachments is 
confidential and may be legally privileged. If you are not the 
intended recipient you are hereby notified that you must not use, 
disclose, distribute, copy, print or rely on this email's content. If 
you are not the intended recipient, please notify the sender 
immediately and then delete the email and any attachments from your 
system.

RNIB has made strenuous efforts to ensure that emails and any 
attachments generated by its staff are free from viruses. However, it 
cannot accept any responsibility for any viruses which are 
transmitted. We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email 
and any attachments are those of the author and do not necessarily 
represent those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk 

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


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



<Prev in Thread] Current Thread [Next in Thread>