2009-03-30 12 views
9

Aquí está un artículo trivial, pero válida Docbook:selección XPath en elementos con espacios de nombres

<?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> 

Aquí hay una hoja de estilo que selecciona title si quito el xmlns atribuyen más arriba, y si no lo dejo en:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<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> 

¿Cómo hablo XPath para seleccionar title hasta article si tiene ese atributo de espacio de nombres?

+0

Ver [esta cuestión] (http://stackoverflow.com/questions/103576/whats-wrong-with- my-xpath-xml) – ripper234

Respuesta

15

es necesario agregar un alias para el espacio de nombres y usar ese alias en su 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> 
+0

Brillante, señor. Gracias. –

Cuestiones relacionadas