2

Here is a piece of XML document:

<book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
</book>    

I'm asked to find out the authors whose lastname begin with a capital "C" using XPath. It's simple in this question because there is only one qualified and I can just use the function substring-after() after a whitespace and then check whether it begins with "C". But there is also a possibility that this guy has a very long name, thus middlename can appear, such as Kurt Van Persie Cagle. How can I strip out exactly the substring after the last whitespace?

Please explain and use the functions in XPath.

2
  • What XPath version are you using? Oct 25, 2012 at 0:03
  • i think it is XPath 1.0 @KirillPolishchuk Oct 25, 2012 at 10:51

3 Answers 3

0

You can use "mess" XPath, e.g. you have limit of 4 word in author:

//author[
    (starts-with(substring-after(., ' '), 'C') and not(contains(substring-after(., ' '), ' ')))
    or
    (starts-with(substring-after(substring-after(., ' '), ' '), 'C') and not(contains(substring-after(substring-after(., ' '), ' '), ' ')))
    or
    (starts-with(substring-after(substring-after(substring-after(., ' '), ' '), ' '), 'C') and not(contains(substring-after(substring-after(substring-after(., ' '), ' '), ' '), ' ')))
]

Input:

<book>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>James Linn</author>
    <author>Kurt Van Persie Cagle</author>
</book>

Above XPath will select 2 authors: Kurt Cagle and Kurt Van Persie Cagle. You can extend this XPath to match authors having 5 words and so on... :)

1
  • I've read your solution and I think it just solves the problem using XPath 1.0. Thank you very much! In case of a royal name, however, middle names may get very long, which leads to tedious nesting structure in XPath 1.0 expression. Using XSLT 2.0 seems to solve the problem neater, which I'll learn in next chapter. : D Oct 26, 2012 at 21:54
0

I'm asked to find out the authors whose lastname begin with a capital "C" using XPath.

In general this is not possible to select with a single XPath 1.0 expression. This, of course, can be done using XSLT 1.0.

With XPath 2.0:

/*/author[starts-with(tokenize(., ' ')[last()], 'C')]

XSLT 2.0 - based verification:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:sequence select="/*/author[starts-with(tokenize(., ' ')[last()], 'C')]"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt van Persy Cantor Bagle</author>
    <author>Kurt van Persy Cantor Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
</book>

the XPath expression is evaluated and the selected node(s) are copied to the output:

<author>Kurt van Persy Cantor Cagle</author>
1
  • Thanks for your solution! I haven't learned XSLT yet, and the tokenize() in XPath 2.0 either. But your solution seems neater. I'm currently learning XML basics. I will come back to your solution later. : D Oct 26, 2012 at 21:58
0

Following up on the excellent solution by @DimitreNovatchev, note that you can use the same tokenize concept in XSLT 1.0 if your parser has the ability to use EXSLT's string extension functions.

For example, this EXSLT-enabled XSLT 1.0 solution:

<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:str="http://exslt.org/strings"
  exclude-result-prefixes="str"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:copy-of
      select="/*/author[starts-with(str:tokenize(., ' ')[last()], 'C')]" />
  </xsl:template>

</xsl:stylesheet>

...produces the same desired result when applied to @Dimitre's modified input XML:

<author>Kurt van Persy Cantor Cagle</author>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.