xsltproc 不按名称选择元素 [英] xsltproc doesn't select elements by name

查看:36
本文介绍了xsltproc 不按名称选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XSLT 样式表转换 XHTML,但我什至无法获得基本样式表来匹配任何内容.我确定我遗漏了一些简单的东西.

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

这是我的 XHTML 源文档(没什么大惊小怪的):

Here's my XHTML source 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>

实际内容并不重要,我将在下面演示.顺便说一句,我很确定该文档格式良好,因为它是通过 tidy -asxml 创建的.

The actual contents don't matter too much, as I'll demonstrate below. By the way, I'm pretty sure the document is well-formed since it was created via tidy -asxml.

我更复杂的 XPath 表达式没有返回任何结果,所以作为一个健全的测试,我试图使用以下样式表非常简单地转换它:

My more complex XPath expressions were not returning any results, so as a sanity test, I'm trying to transform 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>

转换是通过 xsltproc --nonet stylesheet.xsl input.html 完成的,输出是:---[]---"(即,它没有找到html 的子元素).但是,如果我将 for-each 部分更改为:

The transform is done via xsltproc --nonet stylesheet.xsl input.html, and the output is: "---[]---" (i.e., it didn't find a child element of html). However, if I change the for-each section to:

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

然后我得到---[html]---".同样,如果我使用 for-each select="*/*" 我得到---[headbody]---"正如我所期望的那样.

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

为什么它可以通过 * 找到子元素(name() 给出正确的名称),但它不会直接使用元素名称找到它?

Why can it find the child element via * (with name() giving the correct name) but it won't find it using the element name directly?

推荐答案

源 XML 中的 html 元素定义了一个命名空间.您必须将它包含在您的匹配表达式中并在您的 xsl:stylesheet 元素中引用它:

The html element in your source XML defines a namespace. You have to include it in your match expression and reference it in your 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>

这篇关于xsltproc 不按名称选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆