BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
The XSLT Example: Generating a new stylesheet
The XSLT Example: Numbering paragraphs and chapters
The XSLT Example: Creating listboxes and checkboxes using variables
The XSLT Example: Creating an HTML document with 'previous' and 'next' links
The XSLT Example: Generating a new stylesheet
The XSLT Example: Using different axes
The XSLT Example: Creating a summary of author sales for a publisher
The XSLT Example: Creating a summary of all Apollo flights
The XSLT Example:
The XSLT Element: xsl:with-param
The XSLT Element: xsl:when
The XSLT Element: xsl:variable
The XSLT Element: xsl:value-of
The XSLT Element: xsl:transform
The XSLT Element: xsl:text
The XSLT Element: xsl:template
The XSLT Element: xsl:stylesheet
The XSLT Element: xsl:strip-space
The XSLT Element: xsl:sort
The XSLT Element: xsl:processing-instruction
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :645

 
[XSLT Reference] [XPath Reference] [Download Reference Download this Reference]

Example: Combining and intersecting two nodesets

This example shows how you can compare and combine the results in different node-sets. Using the union operator (|) two or more sets can be combined to one set (the nodes that occur in both sets should occur only once in the resulting conbination). Using this combining of node-sets, we can also find the intersection of two node-sets: the technique to do this was presented by Michael Kay and is shown in this example.
XSLT elements used:xsl:stylesheet xsl:template xsl:variable xsl:value-of
XSLT and XPath functions used:count
XML source:<?xml version="1.0"?> <publisher> <books> <book> <title>Stranger in a strange land</title> <ISBN>0441788386</ISBN> <author-ref ref="rh"/> <sold>230000</sold> </book> <book> <title>Starman Jones</title> <ISBN>0345328116 </ISBN> <author-ref ref="rh"/> <author-ref ref="jldr"/> <sold>80000</sold> </book> <book> <title>The Space Merchants</title> <ISBN>02352123456 </ISBN> <author-ref ref="fp"/> <author-ref ref="ck"/> <sold>120000</sold> </book> </books> <authors> <author id="rh"> <first_name>Robert</first_name> <last_name>Heinlein</last_name> </author> <author id="ck"> <first_name>Cyril</first_name> <last_name>Kornbluth</last_name> </author> <author id="fp"> <first_name>Frederick</first_name> <last_name>Pohl</last_name> </author> <author id="jldr"> <first_name>Judy-Lyn</first_name> <last_name>Del Rey</last_name> </author> </authors> </publisher>
XSLT code:<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="books_by_rh" select="//book[author-ref/@ref='rh']"/> <xsl:variable name="books_by_more_than_one_author" select="//book[count(author-ref) &gt; 1]"/> <xsl:template match="/"> Total number of books by Robert Heinlein: <xsl:value-of select="count($books_by_rh)" /> Total number of books by several authors: <xsl:value-of select="count($books_by_more_than_one_author)" /> Number of books in the union of the two sets <xsl:value-of select="count($books_by_rh|$books_by_more_than_one_author)"/> Number of books in the intersection of the two sets <xsl:value-of select="count($books_by_rh[count(.|$books_by_more_than_one_author)= count($books_by_more_than_one_author)])"/> </xsl:template> </xsl:stylesheet>
Resulting XML output:
saxon
xt
xalan
msxml3
msxml.NET
<?xml version="1.0" encoding="utf-8"?> Total number of books by Robert Heinlein: 2 Total number of books by several authors: 2 Number of books in the union of the two sets 3 Number of books in the intersection of the two sets 1<?xml version="1.0" encoding="utf-8"?> Total number of books by Robert Heinlein: 2 Total number of books by several authors: 2 Number of books in the union of the two sets 3 Number of books in the intersection of the two sets 1<?xml version="1.0" encoding="UTF-8"?> Total number of books by Robert Heinlein: 2 Total number of books by several authors: 2 Number of books in the union of the two sets 3 Number of books in the intersection of the two sets 1<?xml version="1.0" encoding="UTF-16"?> Total number of books by Robert Heinlein: 2 Total number of books by several authors: 2 Number of books in the union of the two sets 3 Number of books in the intersection of the two sets 1<?xml version="1.0" encoding="utf-8"?> Total number of books by Robert Heinlein: 2 Total number of books by several authors: 2 Number of books in the union of the two sets 3 Number of books in the intersection of the two sets 1

Note: You can now download the full content of the XPath reference, the XSLT reference and the DOM reference in one PDF document. This document contains the implementation tables, internal links to navigate through the references, a full linked table of contents. You can use free text search through the whole reference or print as a nicely formatted document. Download here...

© 2000 Teun Duynstee. Shown on TopXML.com. Information used from XSLT and XPath recommendations © W3C and MSDN documentation © Microsoft.
Do you have comments, additions or suggestions, mail me.

Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C

View all Jobs (Add yours)
View all CV (Add yours)



unlimited conferencing
swimming pool contractor
large conference calls
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP