如何使用 XSLT 从平面 xml 文件创建 html 列表 [英] How to create html list from flat xml file using XSLT

查看:22
本文介绍了如何使用 XSLT 从平面 xml 文件创建 html 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种干净的方法来使用 XSLT 执行以下操作.

转换此来源:

 blah blah</para><list>num1</list><list>num2</list><list>num3</list><para>blah blah</para><list>num1</list><list>num2</list><para>blah blah blah blah blah</para>

到这个输出:

<p>blah blah</p><ol><li>num1</li><li>num2</li><li>num3</li></ol><p>blah blah</p><ol><li>num1</li><li>num2</li></ol><p>等等等等等等等等</p>

请记住,我不知道到底有多少 .

到目前为止,我有这个:

<p><xsl:value-of select="."/></p></xsl:模板><xsl:template match="list"><ol><li><xsl:value-of select="."/></li></ol></xsl:模板>

但我的输出是这样的:

<p>blah blah</p><ol><li>num1</li></ol><ol><li>num2</li></ol><ol><li>num3</li></ol><p>blah blah</p><ol><li>num1</li></ol><ol><li>num2</li></ol><p>等等等等等等等等</p>

我知道为什么我会收到重复的

    元素,但我不知道如何阻止它.脑筋急转弯.

    任何帮助将不胜感激.

    解决方案

    XSLT 2.0 有专门用于此类操作的工具:

    <xsl:template match="xml"><xsl:for-each-group select="*" group-adjacent="boolean(self::list)"><xsl:when test="current-grouping-key()"><ol><xsl:apply-templates select="current-group()"/></ol></xsl:when><xsl:否则><xsl:apply-templates select="current-group()"/></xsl:否则></xsl:选择></xsl:for-each-group></xsl:模板><xsl:template match="para"><p><xsl:apply-templates/></p></xsl:模板><xsl:template match="list"><li><xsl:apply-templates/></xsl:模板></xsl:stylesheet>

    使用此 XML:

    <para>blah blah</para><list>num1</list><list>num2</list><list>num3</list><para>blah blah</para><list>num1</list><list>num2</list><para>blah blah blah blah blah</para></xml>

    您将获得所需的输出:

    <p>blah blah</p><ol><li>num1</li><li>num2</li><li>num3</li></ol><p>blah blah</p><ol><li>num1</li><li>num2</li></ol><p>等等等等等等等等</p>

    您应该在 http://www.w3.org/TR/xslt20/#xsl-for-each-group

    I am looking for a clean way to do the following using XSLT.

    Convert this source:

    <para>blah blah</para>
    <list>num1</list>
    <list>num2</list>
    <list>num3</list>
    <para>blah blah</para>
    <list>num1</list>
    <list>num2</list>
    <para>blah blah blah blah blah</para>
    

    To this output:

    <p>blah blah</p>
    <ol>
        <li>num1</li>
        <li>num2</li>
        <li>num3</li>
    </ol>
    <p>blah blah</p>
    <ol>
        <li>num1</li>
        <li>num2</li>
    </ol>
    <p>blah blah blah blah blah</p>
    

    Keep in mind I do not know exactly how many <list>'s there will be.

    So far I have this:

    <xsl:template match="para">
        <p><xsl:value-of select="." /></p>
    </xsl:template>
    
    <xsl:template match="list">
        <ol><li><xsl:value-of select="." /></li></ol>
    </xsl:template>
    

    But my output looks like this:

    <p>blah blah</p>    
    <ol><li>num1</li></ol>
    <ol><li>num2</li></ol>
    <ol><li>num3</li></ol>
    <p>blah blah</p>
    <ol><li>num1</li></ol>
    <ol><li>num2</li></ol>
    <p>blah blah blah blah blah</p>
    

    I know why I am getting duplicate <ol> elements, but I do not know how to stop it. Quite a brain teaser.

    Any help would be greatly appreciated.

    解决方案

    XSLT 2.0 has tools especially for this kind of Operations:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="xml">
            <xsl:for-each-group select="*" group-adjacent="boolean(self::list)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <ol>
                            <xsl:apply-templates select="current-group()"/>
                        </ol>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:template>
        <xsl:template match="para">
            <p>
                <xsl:apply-templates/>
            </p>
        </xsl:template>
        <xsl:template match="list">
            <li>
                <xsl:apply-templates/>
            </li>
        </xsl:template>
    </xsl:stylesheet>
    

    With this XML:

    <xml>
        <para>blah blah</para>
        <list>num1</list>
        <list>num2</list>
        <list>num3</list>
        <para>blah blah</para>
        <list>num1</list>
        <list>num2</list>
        <para>blah blah blah blah blah</para>
    </xml>
    

    You'll get the desired Output:

    <p>blah blah</p>
    <ol>
        <li>num1</li>
        <li>num2</li>
        <li>num3</li>
    </ol>
    <p>blah blah</p>
    <ol>
        <li>num1</li>
        <li>num2</li>
    </ol>
    <p>blah blah blah blah blah</p>
    

    You should read up on for-each-group at http://www.w3.org/TR/xslt20/#xsl-for-each-group

    这篇关于如何使用 XSLT 从平面 xml 文件创建 html 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆