【问题标题】:Tricky nested list XSLT棘手的嵌套列表 XSLT
【发布时间】:2023-03-07 02:07:02
【问题描述】:

我是 XSLT 的新手,对格式化列表有点困惑。基本上我需要我的 XML 结构,其中有一部分:

<slideshow>
    <slide id="A1">
        <title>XML techniques</title>
        <paragraph> Slideshow prepresents different kind of <bold>XML</bold> techniques </paragraph>
        <paragraph> Most common XML Techniques are </paragraph>
        <numberedlist>
            <item> Basic XML, DTD (version 1.0) </item>
            <item> XHTML </item>
            <itemizedlist>
                <item> XHTML 1.0 </item>
                <item> XHTML basic </item>
                <numberedlist>
                    <item> for mobile phones </item>
                    <item> basic set for all XHTML documents</item>
                </numberedlist>
            </itemizedlist>
            <item> XML namespace </item>
            <item> XSL </item>
            <itemizedlist>
                <item> XSLT - template based programming language</item>
                <item> XSL-FO - formating output like CSS </item>
            </itemizedlist>

            <item> Programming API (like SAX and DOM) </item>
            <item> XML Schemas </item>
        </numberedlist>
    </slide>
..
</slideshow>

看起来像这样:

  1. 基本 XML、DTD(1.0 版)
    • XHTML 1.0
    • XHTML 基础知识
      1. 用于手机
      2. 所有 XHTML 文档的基本设置
  2. XHTML
  3. XML 命名空间
  4. XSL
    • XSLT - 基于模板的编程语言
    • XSL-FO - 像 CSS 一样格式化输出
  5. 编程 API(如 SAX 和 DOM)
  6. XML 模式

我想尽可能简单地做到这一点,所以我只使用模板而不使用复杂的 XPath 掩码,但似乎没有简单的方法。有人可以帮忙吗?谢谢!

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    实际上,这在 XSLT 中是微不足道的,因为它是递归的 processing model

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="numberedlist">
        <ol>
            <xsl:apply-templates/>
        </ol>
    </xsl:template>
    
    <xsl:template match="itemizedlist">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    
    <xsl:template match="item">
        <li>
            <xsl:value-of select="." />
        </li>
    </xsl:template>
    
    <xsl:template match="text()"/>
    
    </xsl:stylesheet>
    

    结果

    <ol>
       <li> Basic XML, DTD (version 1.0) </li>
       <li> XHTML </li>
       <ul>
          <li> XHTML 1.0 </li>
          <li> XHTML basic </li>
          <ol>
             <li> for mobile phones </li>
             <li> basic set for all XHTML documents</li>
          </ol>
       </ul>
       <li> XML namespace </li>
       <li> XSL </li>
       <ul>
          <li> XSLT - template based programming language</li>
          <li> XSL-FO - formating output like CSS </li>
       </ul>
       <li> Programming API (like SAX and DOM) </li>
       <li> XML Schemas </li>
    </ol>
    

    渲染:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多