?
Solved

Ignore some elements in XSLT for-each loop

Posted on 2006-07-11
2
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

770 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