Solved

Ignore some elements in XSLT for-each loop

Posted on 2006-07-11
2
375 Views
Last Modified: 2013-11-19
I have an XML structure where one of the attributes of an element can be used more than once.  I want to have an XSLT that goes through each element and only picks out the first occurrences of the attribute in that element.

For instance,
Here is my XML:

<Node name="NodeA" type="Fixed" />
<Node name="NodeB" type="Fixed" />
<Node name="NodeC" type="Fixed" />
<Node name="NodeE" type="Pin" />
<Node name="NodeF" type="Fixed" />
<Node name="NodeG" type="Float" />
<Node name="NodeH" type="Float" />
<Node name="NodeI" type="Pin/>

And I want to use an XSLT to sort the elements and only pick out the first occurrences of each, therefore my resulting XML would be:

<Node name="NodeA" type="Fixed" />
<Node name="NodeE" type="Pin" />
<Node name="NodeG" type="Float" />

Unfortunately I can use a flag variable in an XSLT because I can't reset a variable.  That was the only way I could think of doing it.

<xsl:for-each select="//Node">
<xsl:sort select="@type" />
  <xsl:if ..the type is not the same as the previous... >
     ...Create the XML line here...
  </xsl:if>
     ...Otherwise ignore it...
</xsl:for-each>
0
Comment
Question by:tmar89
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 17083146
Hi tmar89,
> <xsl:for-each select="//Node">

make it <xsl:for-each select="//Node[not(@type = preceding-sibling::Node/@type)]">
and you will only iterate over the unique ones


Cheers!
0
 

Author Comment

by:tmar89
ID: 17083251
Beautiful!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cookies analysis tools 2 106
Download a website to hdd 2 89
What does GoogleTagMgr javascripts below do 5 58
Customizing XML with Powershell 2 32
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question