【问题标题】:XSLT apply-template does not show multiple of same tagXSLT 应用模板不显示多个相同标记
【发布时间】:2014-11-07 13:57:31
【问题描述】:

我正在使用 XSLT 样式表将爱丽丝梦游仙境文本的简单 XML 文件呈现为 HTML。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Alice.xsl"?>


<book>
<title>Alice's Adventures in Wonderland</title>

<text>
<chapter>
<chapter_heading>Chapter I. Down the Rabbit-Hole</chapter_heading>

<p>Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversations?'</p>

<p>So she was considering in her own mind (as well as she could, for the
hot day made her feel very sleepy and stupid), whether the pleasure
of making a daisy-chain would be worth the trouble of getting up and
picking the daisies, when suddenly a White Rabbit with pink eyes ran
close by her.</p>
</chapter>

</text>
</book>

简单的东西。我们正在尝试使用以下代码仅将章节的标题和段落输出到 HTML 中的标签中:

<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="http://www.w3.org/1999/XSL/Transform" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>

<article>
    <xsl:apply-templates/>
</article>

</body>


</html>
</xsl:template>

<xsl:template match="title"/>

<xsl:template match="chapter">
    <h3>
      <xsl:apply-templates select="chapter_heading"/>
    </h3>
    <div class="paragraph">
        <xsl:apply-templates select="p"/>
    </div>
</xsl:template>


</xsl:stylesheet>

但是一旦在浏览器中打开 XML 文件,XML 中的所有单独的“p”标签都会组合到 HTML 中的一个大“div”中。

我们的团队显然对 XSL 非常陌生,但就我们能够研究的情况而言,我们不知道为什么它不能顺利运行。任何帮助将不胜感激!

【问题讨论】:

    标签: html xml xslt xhtml xslt-2.0


    【解决方案1】:

    您没有为&lt;p&gt; 元素定义template。 您使用&lt;xsl:apply-templates select="p"/&gt;&lt;p&gt; 元素上有效地应用了模板,但没有针对它们的特定模板,xslt 处理器仅应用默认模板,仅输出元素的文本内容。这就是为什么您将所有内容都放在同一个 &lt;div class='paragraph'&gt; 元素中(这是您在 &lt;p&gt; 上应用模板之前创建的父元素)。

    我猜,您想保留输入中的&lt;p&gt; 元素,然后只需在下面添加模板声明,您就会在输出中获得&lt;p&gt;

    <xsl:template match="p">
        <xsl:copy-of select="."/>
    </xsl:template>
    

    作为通用设计模板,当您想要复制具有某些布局功能的输入时,请使用具有此默认声明的“复制模板模式”,该声明适用于任何输入(&lt;p&gt; &lt;citation&gt; 等)。

    <!-- This match will select any element without any other template matching (the `<p>` elements in your case.)-->
    <xsl:template match="*">
        <!-- First copy the element itself -->
        <xsl:copy>
           <!-- Then copy the attributes if any. -->
           <xsl:copy-of select="@*">
           <!-- Finally apply-templates on childs, then text nodes will be outputed by default templates, and elements would go through your template flow allowing to be copied or formatted if needed (with specific templates) -->
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 2016-12-12
      • 2014-02-08
      • 1970-01-01
      相关资源
      最近更新 更多