1

我在玩 XML 和 XSL。我有一个看起来像这样的 XMLfile:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/_xslt/xslt_terms.xsl" type="text/xsl" ?>
    <terms>
       <term><p>Use of Website</p>
           <term>
                <term><p>wording here</p></term>
                <term><p>more words!</p></term>
           </term>
        <term><p>serious words</p></term>
    </terms>

和一个看起来像的 XSL 文件

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

<xsl:template match="term">
    <xsl:number level="multiple" format="1. "/>
    <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

输出应显示为

 1. Use of Website
    1.1. 
        1.1.1. wording here
        1.1.2. more words

    1.2. serious words

但是当我跑步时

<cfoutput>
     <cffile action="read" 
             file="#application.sPath#_xslt/xslt_terms.xsl" 
             variable="variables.xmltrans">
     <cfset variables.xmldoc = XmlParse("#application.sPath#_templates/_ajax/_terms/xml_terms.xml")>
     #XMLTransform(variables.xmldoc, variables.xmltrans)#
</cfoutput>

我得到一大块没有换行符的文本。所以它看起来像:

 1. Use of Website 1.1. 1.1.1. wording here 1.1.2. more words 1.2. serious words

正如我所说,这是我第一次使用 XML 和 XSL,因为......很长一段时间,所以我很可能错过了一些东西

编辑

我找到了一些代码来帮助几乎完成这项工作。我已更改我的 xml 以删除所有<p>标签,并且我已将我的 xsl 文件更改为:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

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

<xsl:template match="term">
    <p>
        <xsl:number format="1." level="multiple"/>
        <xsl:apply-templates select="@*|node()"/>
    </p>
</xsl:template>

这几乎可以做到,但我失去了缩进,我能做些什么让它像上面那样缩进吗?

4

1 回答 1

1

如果您可以确保注入 XSLT 输出的完整 HTML 文档可以包含一些 CSS,那么我建议创建一个 HTML 有序嵌套列表,其中的数字使用 CSS 完成:

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

    <xsl:strip-space elements="*"/>
    <xsl:output method="html" indent="yes" version="5.0"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>list test</title>
                <style>
                    ol.nested  { counter-reset: section; list-style-type: none; }
                    ol.nested li { counter-increment: section; }
                    ol.nested li:before { content: counters(section, ".") ". "; }
                </style>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="terms[term]">
        <ol class="nested">
            <xsl:apply-templates/>
        </ol>
    </xsl:template>

    <xsl:template match="term">
        <li>
            <xsl:apply-templates select="node()[not(self::term)]"/>
            <xsl:if test="term">
                <ol class="nested">
                    <xsl:apply-templates select="term"/>
                </ol>
            </xsl:if>
        </li>
    </xsl:template>
</xsl:stylesheet>

输入 XML 是

<?xml-stylesheet type="text/xsl" href="sheet.xsl"?>
<terms>
    <term><p>Use of Website</p>
        <term>
            <term><p>wording here</p></term>
            <term><p>more words!</p></term>
        </term>
        <term><p>serious words</p></term>
    </term>
</terms>

现代浏览器会将其呈现为带有您想要的计数的嵌套列表。

如果您想使用 XSLT 创建数字,那么我仍然会创建一个 HTML 列表,因为 HTML 需要结构化:

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

    <xsl:strip-space elements="*"/>
    <xsl:output method="html" indent="yes" version="5.0"/>

    <xsl:template match="terms[term]">
        <ol style="list-style-type: none;">
            <xsl:apply-templates/>
        </ol>
    </xsl:template>

    <xsl:template match="term">
        <li><xsl:number level="multiple" format="1. "/>
            <xsl:apply-templates select="node()[not(self::term)]"/>
            <xsl:if test="term">
                <ol style="list-style-type: none;">
                    <xsl:apply-templates select="term"/>
                </ol>
            </xsl:if>
        </li>
    </xsl:template>
</xsl:stylesheet>

现在当应用于输入时

<?xml-stylesheet type="text/xsl" href="sheet.xsl"?>
<terms>
    <term><p>Use of Website</p>
        <term>
            <term><p>wording here</p></term>
            <term><p>more words!</p></term>
        </term>
        <term><p>serious words</p></term>
    </term>
</terms>

你得到你想要的编号。

于 2013-10-08T17:14:29.803 回答