[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: xsl array alternatives (novice)

Subject: RE: xsl array alternatives (novice)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 18 Sep 2002 16:12:40 -0400
xsl array
Isaac,

At 03:41 PM 9/18/2002, Stuart wrote:
> Exhibit A. --before
> (many other irrelevent records)...
> <nodeA att1="True" att2="N" att3="1234"/>
> <nodeA att1="True" att2="N" att3="321"/>
> <nodeA att1="True" att2="N" att3="456"/>
> <nodeA att1="True" att2="Y" att3="9876"/>
> <nodeB att1="True" att5="Y" att3="45"/>
> <nodeB att1="True" att5="N" att3="65"/>
> <nodeC att1="True" att7="Y" att3="784"/>
> <nodeC att1="True" att7="Y" att3="412"/>
> <nodeC att1="True" att7="N" att3="635"/>
> ...(many other irrelevent records)
>
> I would like to create xml output that looks something like this:
>
> Exhibit B. --after
> (many irrelevent records)...
> <nodeA att1="True" att2="N" att3="1234"/>
> <nodeA att1="True" att2="N" att3="321"/>
> <nodeA att1="True" att2="N" att3="456"/>
> <nodeB att1="True" att5="N" att3="65"/>
> <nodeC att1="True" att7="N" att3="635"/>
> ...(many irrelevent records that may or may not have duplicate attribute
> names and values)
> <nodeA att1="True" att2="Y" att3="9876"/>
> <nodeB att1="True" att5="Y" att3="45"/>
> <nodeC att1="True" att7="Y" att3="784"/>
> <nodeC att1="True" att7="Y" att3="412"/>
> <end of document/>

Why not use a key:

<xsl:key name="getNodes" match="nodeA|nodeB|nodeC" use="@*"/>

Then all you need to do is:

<xsl:copy-of select="key('getNodes','N')"/>
<xsl:copy-of select="key('getNodes','Y')"/>

This works fine, and in many cases may be the best way to approach it.


A simpler solution is just to filter the elements out with select attributes on apply-templates (or copy-of). This avoids the overhead of the key (as if that were frequently a problem: it's not):

<xsl:template match="parent-of-nodes">
  <xsl:copy-of select="*[@* = 'N']"/>
  <xsl:copy-of select="*[@* = 'Y']"/>
</xsl:template>

Like Stuart's solution, this assumes that any attribute marked as 'N' pulls the node into the N category, any attribute marked 'Y' pulls it into the Y category. It depends on the rule for equality of node sets with strings (they are equal if the node set -- here, the set of all an element's attributes -- contains any node equal to the string.) The logic can easily be tightened, of course.

This is a pretty basic application of XSLT; as you can see no procedural thinking (such as where you're going to "put" these nodes, as in an array) is necessary at all.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.