Solved

Ignore some elements in XSLT for-each loop

Posted on 2006-07-11
2
340 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now