Solved

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

Posted on 2002-03-14
3
337 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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.
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. 
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

27 Experts available now in Live!

Get 1:1 Help Now