RE: [xsl] xsl:for-each...iteration question

Subject: RE: [xsl] xsl:for-each...iteration question
From: cknell@xxxxxxxxxx
Date: Tue, 19 Apr 2005 13:14:07 -0400
This suggests a two-stage, pipeline processing model to me. The first stage would take your prohibited attribute name XML document and produce a second style sheet to process the "input doc". 

For the first stylesheet you have to fiddle with the namespaces so as not to confuse the processor. A good example of this is shown on page 255 of XSLT Programmer's Reference, 2nd Edition.

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

<xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl" />

<xsl:template match="/exclude-list">
  <xslt:choose>
    <xsl:apply-templates />
    <xslt:otherwise><xslt:copy-of select="." /></xslt:otherwise>
  </xslt:choose>
</xsl:template>

<xsl:template match="exclude-attr">
  <xslt:when test="." />
</xsl:template>

The stylesheet produced by the first transformation (and therefore is the stylesheet that processes "input doc") would have a construct like this:

<xsl:choose>
  <xsl:when test="123" />
  <xsl:when test="456" />
  <xsl:otherwise><xsl:copy-of select="." /></xsl:otherwise>
<xsl:choose>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Paul Coletti <pcoletti@xxxxxxxxxx>
Sent:     Tue, 19 Apr 2005 18:19:29 +0200
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  [xsl] xsl:for-each...iteration question

This has been answered before I'm sure but doing a relevant search in the archives is tricky.
 
I've an input doc containing a number of modify-attr elements
<modify>
  <modify-attr attr-name = "abc"/> 
  <modify-attr attr-name = "123"/>
  <modify-attr attr-name = "789"/>
</modify>
 
I have another document containing a list of prohibited attributes.
<exclude-list>
  <exclude-attr>123</exclude-attr>
</exclude-list>
 
 
I want to iterate over my input doc and copy all those modify-attr elements that are NOT in the exclude list to the output.
 
<xsl:template match="modify/modify-attr">
  <xsl:variable name="currentAttr" select="@attr-name"/>
  <xsl:variable name="currentNode" select="."/>
 
  <xsl:for-each select="document('excludedoc')/exclude-list/exclude-attr">
    <xsl:when test="$currentAttr=.">
      <xsl:message>Exclude</xsl:message>
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of select="$currentNode"/>
    </xsl:otherwise>
  </xsl:for-each>
 
</xsl:template>
 
Obvisouly, this is flawed, I get multiple modify-attr elements copied to the output because I cannot see a way of ensuring the for-each only copies a permitted node once and once only.
 
I've a feeling this is approaching the problem from the wrong way....

Current Thread