for-each中的XSLT应用模板

问题描述 投票:0回答:1

我正在尝试为Simple Docbook翻译器编写一个简单的XHTML(输入XHTML是一个有限的子集,因此应该可行)。>>

我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" standalone="no"/>
    <!--
    <xsl:strip-space elements="*"/>
    -->

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <!-- skip implicit tags-->
    <xsl:template match="/html/body"><xsl:apply-templates/></xsl:template>
    <xsl:template match="/html"><xsl:apply-templates/></xsl:template>

    <!-- paragraphs to sections converter -->
    <xsl:template match="h2">
        <xsl:variable name="title" select="generate-id(.)"/>
        <section>
            <title><xsl:apply-templates select="text()"/></title>
            <xsl:for-each select="following-sibling::*[generate-id(preceding-sibling::h2[1]) = $title and not(self::h2)]">
                <xsl:apply-templates/>
            </xsl:for-each>
        </section>
    </xsl:template>

    <xsl:template match="p">
        <para><xsl:apply-templates select="*|text()"/></para>
    </xsl:template>
    <xsl:template match="p[preceding-sibling::h2]"/>
    <xsl:template match="ul">
        <itemizedlist><xsl:apply-templates select="li"/></itemizedlist>
    </xsl:template>
    <xsl:template match="ul[preceding-sibling::h2]"/>
    <xsl:template match="ol">
        <orderedlist><xsl:apply-templates select="li"/></orderedlist>
    </xsl:template>
    <xsl:template match="ol[preceding-sibling::h2]"/>
    <xsl:template match="li">
        <listitem><para><xsl:apply-templates select="*|text()"/></para></listitem>
    </xsl:template>
</xsl:stylesheet>

对于此输入

<html>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<h2>First title</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<h2>Second title</h2>
<p>First paragraph</p>
<ul>
    <li>A list item</li>
    <li>Another list item</li>
</ul>
<p>Second paragraph</p>
</body>
</html>

我希望这个输出

<para>First paragraph</para>
<para>Second paragraph</para>
<section>
    <title>First title</title>
    <para>First paragraph</para>
    <para>Second paragraph</para>
    <para>Third paragraph</para>
</section>
<section>
    <title>Second title</title>
    <para>First paragraph</para>
    <itemizedlist>
        <listitem>A list item</listitem>
        <listitem>Another list item</listitem>
    </itemizedlist>
    <para>Second paragraph</para>
</section>

但是我知道

<para>First paragraph</para>
<para>Second paragraph</para>
<section><title>First title</title>First paragraphSecond paragraphThird paragraph</section>



<section><title>Second title</title>First paragraph
    <listitem><para>A list item</para></listitem>
    <listitem><para>Another list item</para></listitem>
Second paragraph</section>

由于某些原因,我的段落和列表的模板未应用。我猜是因为匹配的模板是空模板,但是我需要那些模板来防止section之外的重复标签。

我该如何进行这项工作? TIA。

我正在尝试为Simple Docbook翻译器编写一个简单的XHTML(输入XHTML是一个有限的子集,因此应该可行)。我有这个:<...>]

html xml xslt docbook
1个回答
0
投票

使用

        <xsl:for-each select="following-sibling::*[generate-id(preceding-sibling::h2[1]) = $title and not(self::h2)]">
            <xsl:apply-templates select="."/>
        </xsl:for-each>
© www.soinside.com 2019 - 2024. All rights reserved.