문제

I'm new to XSLT and trying to convert an atom XML file extracted from the W3C news feed into HTML. The XML file is shown below:

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

All I want for now is to have the title in each entry as a list. The XSLT I wrote is shown below:

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

After running Xalan, the generated HTML seems to contain all values at every leaf node instead of listing the titles of all entries. Not sure what's wrong with my code here. Thank you.

도움이 되었습니까?

해결책

This is a namespace issue. You've defined templates that match elements with the local names feed and entry that are not in a namespace, but your source document has them in the http://www.w3.org/2005/Atom namespace. Therefore your templates don't match anything, and the default template rules are used instead, which will result in outputting all the text nodes in the whole source document.

I see that you have defined the p prefix in your stylesheet bound to the http://www.w3.org/2005/Atom namespace, so you simply need to use that prefix in your match patterns and select expressions.

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

Note that I've also constrained the apply-templates to pick out just the entry elements, so you don't get the text of the global title, id, generator, etc. as well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top