【问题标题】:Xalan XSLT Processing Output UnexpectedXalan XSLT 处理输出意外
【发布时间】:2014-03-02 05:28:31
【问题描述】:

我是 XSLT 的新手,正在尝试将从 W3C 新闻提要中提取的 atom XML 文件转换为 HTML。 XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>W3C News</title>
    <link rel="alternate" type="text/html" href="http://www.w3.org/" />
    <link rel="self" type="application/atom+xml" href="http://www.w3.org/News/atom.xml" />
    <id>tag:www.w3.org,2008-09-29://4</id>
    <updated>2013-09-06T16:31:45Z</updated>

    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.34-en</generator>


<entry>
    <title>WebCrypto Key Discovery Working Draft Published</title>
    <link rel="alternate" type="text/html" href="http://www.w3.org/News/2013.html#entry-9920" />
    <id>tag:www.w3.org,2013://4.9920</id>

    <published>2013-08-22T16:40:18Z</published>
    <updated>2013-08-22T16:40:18Z</updated>

    <summary>The Web Cryptography Working Group has published a Working Draft of WebCrypto Key Discovery. This specification describes a JavaScript API for discovering named, origin-specific pre-provisioned cryptographic keys for use with the Web Cryptography API. Pre-provisioned keys are keys which have...</summary>
    <author>
        <name>W3C Staff</name>
    </author>

        <category term="Publication" scheme="http://www.sixapart.com/ns/types#category" />

        <category term="Web Design and Applications" scheme="http://www.sixapart.com/ns/types#category" />


    <content type="html" xml:lang="en" xml:base="http://www.w3.org/">

        <![CDATA[<p>The <a href="http://www.w3.org/2012/webcrypto/">Web Cryptography Working Group</a> has published a Working Draft of <a href="http://www.w3.org/TR/2013/WD-webcrypto-key-discovery-20130822/">WebCrypto Key Discovery</a>. This specification describes a JavaScript API for discovering named, origin-specific pre-provisioned cryptographic keys for use with the Web Cryptography API. Pre-provisioned keys are keys which have been made available to the user agent by means other than the generation, derivation, importation functions of the Web Cryptography API. Origin-specific keys are keys that are available only to a specified origin. Named keys are identified by a name assumed to be known to the origin in question and provisioned with the key itself. Learn more about the <a href="http://www.w3.org/Security/">Security Activity</a>.</p>]]>
    </content>
</entry>

<entry>
    <title>Three RDFa Recommendations Published</title>
    <link rel="alternate" type="text/html" href="http://www.w3.org/News/2013.html#entry-9919" />
    <id>tag:www.w3.org,2013://4.9919</id>

    <published>2013-08-22T16:15:02Z</published>
    <updated>2013-08-22T16:15:02Z</updated>

    <summary> The RDFa Working Group today published three RDFa Recommendations. RDFa lets authors put machine-readable data in HTML documents. Using RDFa, authors may turn their existing human-visible text and links into machine-readable data without repeating content. Today&apos;s publications were: HTML+RDFa...</summary>
    <author>
        <name>W3C Staff</name>
    </author>

        <category term="Publication" scheme="http://www.sixapart.com/ns/types#category" />

        <category term="Semantic Web" scheme="http://www.sixapart.com/ns/types#category" />


    <content type="html" xml:lang="en" xml:base="http://www.w3.org/">

        <![CDATA[<p>
<a href="/2001/sw/" class="imageLink">
<img src="http://www.w3.org/Icons/SW/sw-cube.png" alt="Semantic Web Cube"/>
</a>
The <a href="/2010/02/rdfa/">RDFa Working Group</a> today published three RDFa Recommendations. RDFa lets authors put machine-readable data in HTML documents. Using RDFa, authors may turn their existing human-visible text and links into machine-readable data without repeating content. Today's publications were:</p>

<ul class="show_items">
<li><a href="http://www.w3.org/TR/2013/REC-html-rdfa-20130822/">HTML+RDFa 1.1</a>, 
which defines rules and guidelines for adapting the RDFa Core 1.1 and RDFa Lite 1.1 specifications for use in HTML5 and XHTML5. The rules defined in this specification not only apply to HTML5 documents in non-XML and XML mode, but also to HTML4 and XHTML documents interpreted through the HTML5 parsing rules.</li>
<li>The group also published two Second Editions for <a href="http://www.w3.org/TR/2013/REC-rdfa-core-20130822/">RDFa Core 1.1</a> and <a href="http://www.w3.org/TR/2013/REC-xhtml-rdfa-20130822/">XHTML+RDFa 1.1</a>, folding in the errata reported by the community since their publication as Recommendations in June 2012; all changes were editorial.</li>
<li>The group also updated the a <a href="/TR/2013/NOTE-rdfa-primer-20130822/">RDFa 1.1 Primer</a>.</li>
</ul>
<p>Learn more about the <a href="/2001/sw/">Semantic Web Activity</a>.</p>]]>
    </content>
</entry>

</feed>

我现在想要的只是将每个entry 中的title 作为一个列表。我写的XSLT如下图:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:p="http://www.w3.org/2005/Atom"
    xmlns="http://www.w3.org/1999/xhtml"
    version="1.0">

    <xsl:output method="xml" 
        omit-xml-declaration="yes"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

    <xsl:template match="feed">
        <html>
            <body>
                <h1>W3C Atom Document</h1>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="entry">
        <li>
            <xsl:value-of select="title" />
        </li>
    </xsl:template>
</xsl:stylesheet>

运行 Xalan 后,生成的 HTML 似乎包含每个叶节点的所有值,而不是列出所有条目的标题。不确定我的代码有什么问题。谢谢。

【问题讨论】:

    标签: xml xslt xslt-1.0 atom-feed xalan


    【解决方案1】:

    这是一个命名空间问题。您已经定义了与本地名称 feedentry 匹配元素的模板不在命名空间中,但您的源文档在 http://www.w3.org/2005/Atom 命名空间中包含它们。因此,您的模板不匹配任何内容,而是使用默认模板规则,这将导致输出整个源文档中的所有文本节点。

    我看到您已经在绑定到 http://www.w3.org/2005/Atom 命名空间的样式表中定义了 p 前缀,因此您只需在匹配模式中使用该前缀并选择表达式。

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:p="http://www.w3.org/2005/Atom"
        xmlns="http://www.w3.org/1999/xhtml"
        version="1.0">
    
        <xsl:output method="xml" 
            omit-xml-declaration="yes"
            doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
            doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
    
        <xsl:template match="p:feed">
            <html>
                <body>
                    <h1>W3C Atom Document</h1>
                    <ul>
                        <xsl:apply-templates select="p:entry"/>
                    </ul>
                </body>
            </html>
        </xsl:template>
    
        <xsl:template match="p:entry">
            <li>
                <xsl:value-of select="p:title" />
            </li>
        </xsl:template>
    </xsl:stylesheet>
    

    请注意,我还限制 apply-templates 只选择 entry 元素,因此您不会获得全局 titleidgenerator 等的文本好吧。

    【讨论】:

    • 哦,对了。我想如果我在标题中定义了该名称空间,那么我会自动在 XSLT 正文中引用它。规则误解。并感谢您的 select="p:entry 提醒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2012-03-03
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2018-09-19
    相关资源
    最近更新 更多