模板XSLT中的模板

时间:2016-01-01 11:40:13

标签: xslt

   <article>
    <date>28/06/2000 12:30</date>
    <title>Rescued penguins swim home</title>
    <para><place>Cape Town</place> Some 150 penguins unaffected by the oil spill began their long swim from Port Elizabeth in the Eastern Cape back to their breeding habitat at Robben Island near Cape Town on Wednesday. </para>

    <para>The penguins, who have all been tagged, were transported in a truck hired by the <company>South African National Conservation of Coastal Birds (Sanccob)</company> to Port Elizabeth on Tuesday night. </para>

    <para>Its not known how many more birds will be released from Port Elizabeth after receiving treatment. </para>

    <para>More than <link ref="www.newsrus.com/oilspill.html">400 tons of fuel oil escaped from the bulk ore carrier Treasure</link> before divers were able to seal the holds. </para>

    <para>The ship was carrying 130 000 tons of iron ore and 1 300 tons of fuel oil when she sank off the Cape West coast last Friday. </para>

    <para>A spokesperson for <company>Sanccob</company>, Christina Pretorius said the centre had a capacity to treat 1 000 penguins but presently there were in excess of 4 500 birds being rehabilitated and more would be brought to the centre on Wednesday. </para>
    <source>John Rolfe</source>
    </article>

我尝试做这样的页面enter image description here

但我不能在虚拟模板中使用模板,我在XSLT中

<xsl:template match="para">
            <xsl:value-of select="text()" />
    </xsl:template>

让我可以使用这个模板

<xsl:template match="place">
        <h2>
            <xsl:value-of select="text()" />
        </h2>
    </xsl:template>

在para模板中

1 个答案:

答案 0 :(得分:2)

不要在包含混合内容的元素模板中使用<xsl:value-of select="text()"/>,而是确保使用<xsl:apply-templates/>,以便任何子节点都由其模板处理。有一个内置模板复制文本节点,所以你不需要显式输出它们。

所以用

<xsl:template match="place">
  <h2>
    <xsl:apply-templates/>
  </h2>
</xsl:template>

以及您应该没问题的内置模板,将para映射到HTML divsection可能是有意义的,例如

<xsl:template match="para">
  <section>
    <xsl:apply-templates/>
  </section>
</xsl:template>

但只要您确保apply-templates处理子节点,您就可以简单地以模块化方式编写代码,其中每个模板处理某个输入元素并将其映射到某个结果内容。 / p>