Xsltproc does not select items by name

I am trying to convert XHTML using an XSLT stylesheet, but I can't even get a basic stylesheet to fit anything. I am sure that I am missing something simple.

Here is my original XHTML document (no big surprises):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content= "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org" /> ... </body> </html> 

Actual content doesn't matter much, as I will show below. By the way, I am sure that the document is well-formed, since it was created through tidy -asxml .

My more complex XPath expressions did not return any results, since the health test, I am trying to convert it very simply using the following stylesheet:

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>---[</xsl:text> <xsl:for-each select="html"> <xsl:text>Found HTML element.</xsl:text> </xsl:for-each> <xsl:text>]---</xsl:text> </xsl:template> </xsl:stylesheet> 

The conversion is done through xsltproc --nonet stylesheet.xsl input.html , and the output is "--- [] ---" (that is, it did not find the html child). However, if I changed the for-each section to:

 <xsl:for-each select="*"> <xsl:value-of select="name()"/> </xsl:for-each> 

Then I get "--- [html] ---". And similarly, if I use for-each select="*/*" , I get "--- [headbody] ---", as you would expect.

Why can he find the child through * (with name() with the correct name), but he will not find it directly using the name of the element?

+4
source share
3 answers

The html element in the source XML defines a namespace. You must include it in your match expression and reference it in the xsl:stylesheet element:

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml"> <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>---[</xsl:text> <xsl:for-each select="html:html"> <xsl:text>Found HTML element.</xsl:text> </xsl:for-each> <xsl:text>]---</xsl:text> </xsl:template> </xsl:stylesheet> 
+6
source

Change the stylesheet from :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>---[</xsl:text> <xsl:for-each select="html"> <xsl:text>Found HTML element.</xsl:text> </xsl:for-each> <xsl:text>]---</xsl:text> </xsl:template> </xsl:stylesheet> 

before

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.w3.org/1999/xhtml" > <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> <xsl:template match="/"> <xsl:text>---[</xsl:text> <xsl:for-each select="x:html"> <xsl:text>Found HTML element.</xsl:text> </xsl:for-each> <xsl:text>]---</xsl:text> </xsl:template> </xsl:stylesheet> 

Explanation

The XML document declared the default namespace: "http://www.w3.org/1999/xhtml" , and all unlisted nodes that descend from the top element declaring this default namespace belong to this namespace.

In XPath, on the other hand, any unsigned name is considered to belong to "without a namespace."

Therefore, the <xsl:for-each select="html"> command will select and apply its body to all html elements that are referred to as "without a namespace" - and there are none in the document. The only html element belongs to the xhtml namespace.

Decision

Names that belong to the default namespace cannot be specified without changes. Therefore, we need to bind the prefix to the namespace to which this element belongs. If this prefix is "x:" , then we can refer to any such element with a prefix to "x:" .

+4
source

A workaround without a namespace declaration, so the stylesheet accepts any namespace:

 <xsl:template match="*[name()='html']" > 
0
source

All Articles