Solved

XPath to select nodes where child node is absent or empty?

Posted on 2002-03-14
3
334 Views
Last Modified: 2012-08-13
In the following XML, I would like to select all employees without a middle name either because the node is empty or because the node is absent. In this example, the result would be Bill Gates and Larry Elison.

<Employees>
  <Employee>
    <FirstName>Bill</FirstName>
    <MiddleName></MiddleName>
    <LastName>Gates</LastName>
  </Employee>
  <Employee>
    <FirstName>George</FirstName>
    <MiddleName>W</MiddleName>
    <LastName>Bush</LastName>
  </Employee>
  <Employee>
    <FirstName>Larry</FirstName>
    <LastName>Elison</LastName>
  </Employee>
0
Comment
Question by:FrenchJericho
  • 3
3 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6866705
XPath statement
===============
//Employee[string-length(normalize-space(MiddleName)) != 0]
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 50 total points
ID: 6867661
oops misread the question, the XPath is
//Employee[string-length(normalize-space(MiddleName)) = 0 or not(MiddleName)]
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6867694
in fact, the following would be sufficient
//Employee[string-length(normalize-space(MiddleName)) = 0]
XML Document
============
<?xml version="1.0" encoding="iso-8859-1"?>
<Employees>
 <Employee>
   <FirstName>Bill</FirstName>
   <MiddleName></MiddleName>
   <LastName>Gates</LastName>
 </Employee>
 <Employee>
   <FirstName>George</FirstName>
   <MiddleName>W</MiddleName>
   <LastName>Bush</LastName>
 </Employee>
 <Employee>
   <FirstName>Larry</FirstName>
   <LastName>Elison</LastName>
 </Employee>
</Employees>

XSLT Document
-------------
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"
 exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="iso-8859-1" indent="yes" />
<xsl:template match="/">
<Employees>
<xsl:copy-of select="//Employee[string-length(normalize-space(MiddleName)) = 0]" />
</Employees>
</xsl:template>
</xsl:stylesheet>

Output
========
<?xml version="1.0" encoding="iso-8859-1"?>
<Employees>
<Employee>
<FirstName>Bill</FirstName>
<MiddleName></MiddleName>
<LastName>Gates</LastName>
</Employee>
<Employee>
<FirstName>Larry</FirstName>
<LastName>Elison</LastName>
</Employee>
</Employees>
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

13 Experts available now in Live!

Get 1:1 Help Now