xsl-list
[Top] [All Lists]

Re: xsl:for-each...iteration question (XSLT 1.0solution)

2005-04-20 11:40:17
Paul, for my 0.02 Bhat, I'd urge you take another look at what Mukul posted in his response at http://www.biglist.com/lists/xsl-list/archives/200504/msg00911.html . His response is much more XSL-esque than the one below, in that it does the XSL processor do all the heavy lifting. It's in the spirit of what M.D. Peterson gushed is a recent post ;-)

Mukul's solution is identity-transform-based: the identity transform matches any node, and copies input to output verbatin. For nodes which you want specially processed (excluded, in your case) you provide a specifically-matching template (which trumps the identity template, as it were). In this case those specifically-matching templates match your stop words, and don't output anything--effectively removing them from the output doc.

IMO the "procedural" solutiuon below, with explicit tests for stop words, is fine in that it works, but the one outlines at the link above is better.

Regards,

--A

From: "Paul Coletti" <pcoletti(_at_)novell(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: Re: [xsl] xsl:for-each...iteration question (XSLT 1.0solution)
Date: Wed, 20 Apr 2005 09:43:41 +0200

Thanks to all the varied replies. I got a lot out of them.

I went for the one outlined by Jay Bryant below which did the trick. I guess this old assembler coder needs to start thinking in functional-programming terms....

>>> JBryant(_at_)s-s-t(_dot_)com 19/04/2005 17:59:44 >>>
Sorry to reply to my own post. I realized that you won't get a well-formed
XML document with my original solution. Here's the fix:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:variable name="exclude" select="document('excludedoc.xml')"/>

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

  <xsl:template match="modify-attr">
    <xsl:variable name="name" select="@attr-name"/>
    <xsl:if test="not($exclude/exclude-list/exclude-attr[.=$name])">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Of course, you probably have a bunch of other templates to manage anyway,
but I do like to have my answers produce well-formed output.

Re-tested with Saxon 8.4 Xalan-J 2.4.1.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies.




JBryant(_at_)s-s-t(_dot_)com
04/19/2005 11:51 AM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com


To
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
cc

Subject
Re: [xsl] xsl:for-each...iteration question






My solution would go like this:
At the top of the stylesheet, use the document function to write the
exclude-attr elements into a variable (so that I only have to use the
document function once).
In the modify-attr template, see if the current node has a matching
exclude-attr element (by examining the contents of that variable).
If not, write the modify-attr element to the result.

So, I'd end up with this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:variable name="exclude" select="document('excludedoc.xml')"/>

  <xsl:template match="modify-attr">
    <xsl:variable name="name" select="@attr-name"/>
    <xsl:if test="not($exclude/exclude-list/exclude-attr[.=$name])">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Tested with Saxon 8.4 and Xalan-J 2.4.1.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





"Paul Coletti" <pcoletti(_at_)novell(_dot_)com>
04/19/2005 11:19 AM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com


To
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
cc

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....





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar ? get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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