views:

343

answers:

2

Here's a trivial but valid Docbook article:

<?xml version="1.0" encoding="utf-8"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0">
<title>I Am Title</title>
<para>I am content.</para>
</article>

Here's a stylesheet that selects title if I remove the xmlns attribute above, and not if I leave it in:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="article"/>
    </xsl:template>
    <xsl:template match="article">
      <p><xsl:value-of select="title"/></p>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

How do I talk XPath into selecting title through article if it has that namespace attribute?

+5  A: 

You need to add an alias for your namespace and use that alias in your XPath

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:a="http://docbook.org/ns/docbook"
   exclude-result-prefixes="a"
   >
<xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="a:article"/>
    </xsl:template>
    <xsl:template match="a:article">
      <p><xsl:value-of select="a:title"/></p>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>
AnthonyWJones
Brilliant, sir. Thank you.
+1  A: 

See this question

ripper234