XPath and Namespaces Solutions

Assignment 4 — XML Foundations (INFOSYS 242; Fall 2006)

Authors: Erik Wilde / TA: Katrina Rhoads Lindholm

XPath

  1. Select all the references that list an author with a surname of "Wilde.” (Note: You don't have to look for the <names type="sharef:author"> attribute value, just assume that all names contain author names) [37]

    Solutions
    • //reference[.//surname = 'Wilde']
    • //reference[names/person/surname = 'Wilde']
    • /bibliography/reference[names/person/surname="Wilde"]


  2. Select all the references that list an author with a surname of ‘Wilde’ where there was at least one co-author. (Note: You don't have to look for the <names type="sharef:author"> attribute value, just assume that all names contain author names) [17]

    Solutions
    • //reference[.//surname[count(../../person) > 1] = 'Wilde']
    • //reference[names/person/surname = 'Wilde' and names/count(person) > 1]
    • //reference[names/person/surname = "Wilde" and count(names/person) >= 2]
    • //reference[names/person/surname="Wilde" and names/person/surname!="Wilde"]
    • //reference[names/person/surname = "Wilde" and names/person[2]]
    • //reference[.//surname='Wilde'][.//surname!='Wilde']
    • //reference/names[count(person)>1]/person[surname="Wilde"]/../..


  3. Select all the references published in 2003 in Germany [5]

    Solutions
    • //reference[contains(address,'Germany')][starts-with(date/@value,'2003')]
    • //reference[date[contains(@value,"2003")] and address[@type = "sharef:placePublished" and contains(., "Germany")]]
    • //reference [date[contains(@value, "2003")] and contains(address, "Germany")]
    • //reference [date/contains(@value,"2003") and address/contains(. , "Germany")]
    • //reference[contains(address,"Germany")]/date[contains(@value,"2003")]/..
    • //reference[date/substring(@value,1,4) ='2003' and substring-after(address,',')=' Germany']


  4. Select all the surnames of people with the givenname "David" [34]

    Solutions
    • //surname[../givenname = "David"]
    • //givenname[text() = 'David']/../surname
    • //reference/names/person[givenname = "David"]/surname


  5. Select all persons which have published papers at the "The OSI95 Transport Service with Multimedia Support." (Hint: Match the name and crossref attributes on a reference) [11]

    Solutions
    • //reference[@crossref = 'dan94']//person
    • //reference[@crossref = 'dan94']/names/person
    • //reference[attribute::crossref = 'dan94']//person
    • //person[ancestor::reference[@crossref="dan94"]]
    • //reference[@crossref=//reference[title='The OSI95 Transport Service with Multimedia Support']/@name]/names/person


  6. Select all the references that have more than one title element [82]

    Solutions
    • //reference[count(.//title) > 1]
    • //reference[count(title) > 1]
    • //reference[title != title]


  7. Select all the references which are marked as being relevant for the topics "xml" and "xmlschema." (Hint: This information is in the bibtex topic field; search for "xml[0.9]" to see how it works). [3]

    Solutions
    • //reference[contains(field[@type = "bibtex:topic"], 'xml[') and contains(field[@type = "bibtex:topic"], 'xmlschema[')]


Namespaces:

Explain the difference between these three xslt stylesheets (only in terms of their namespace usage):

  • <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    </stylesheet>
  • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    </xsl:stylesheet>
  • <xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
    </xslt:stylesheet>
Solution:

The first example doesn't have a prefix so the XSLT tags within it will have the default namespace with no prefix required on the element names. The second and the third examples have namespace prefix. As a result the "xsl" or "xslt" prefixes will need to be added to the tags.



Explain how the following XSLT could be improved:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<title>test</title>
			</head>
			<body><xsl:apply-templates/></body>
		</html>
	</xsl:template>
	<xsl:template match="person">
		<b xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></b>
	</xsl:template>
</xsl:stylesheet>
Solution:

Rather than declaring the html namespace in two places it would make more sense to declare it only once on the document element. This would make it easier to see which namespaces are used throughout the stylesheet.


please send comments to dret@sims.berkeley.edu
last modification on Friday, 29-Sep-2006 23:51:00 CEST
valid CSS! valid XHTML 1.0!