【问题标题】:Basic XSLT example基本 XSLT 示例
【发布时间】:2010-08-15 22:47:42
【问题描述】:

我刚刚开始使用 XSL 将 XML 转换为 HTML,我希望获得以下帮助以帮助我深入研究。

给定 XML 如下 (A):

<Course Title="SampleCourse">
  <Lesson Title="Overview"/>
  <Section Title="Section1">
    <Lesson Title="S1 Lesson 1" />
    <Lesson Title="S1 Lesson 2" />
  </Section>
  <Section Title="Sections 2">
    <Lesson Title="S2 Lesson 1" />
  </Section>
</Course>

或喜欢(B):

<Course Title="SampleCourse">
  <Section Title="Section1">
    <Lesson Title="S1 Lesson 1" />
    <Lesson Title="S1 Lesson 2" />
  </Section>
  <Section Title="Sections 2">
    <Lesson Title="S2 Lesson 1" />
  </Section>
</Course>

我将如何生成可以将上述示例转换为 (A) 的 XSL 文件:

<h3>SampleCourse</h3>
<ul>
  <li>Overview</li>
  <li>Section1</li>
  <ul>
    <li>S1 Lesson 1</li>
    <li>S1 Lesson 2</li>
  </ul>
  <li>Sections 2</li>
  <ul>
    <li>S1 Lesson 1</li>
  </ul>
</ul>

或(B):

<h3>SampleCourse</h3>
<ul>
  <li>Section1</li>
  <ul>
    <li>S1 Lesson 1</li>
    <li>S1 Lesson 2</li>
  </ul>
  <li>Sections 2</li>
  <ul>
    <li>S1 Lesson 1</li>
  </ul>
</ul>

谢谢!

【问题讨论】:

    标签: xml xslt


    【解决方案1】:
    <xsl:template match="Course"> <!-- We use template to define what shows up when we encounter the element "Course" -->
        <h3><xsl:value-of select="@Title"/></h3> <!-- value-of is used here to grab the title. @ is for attribute. -->
        <ul>
            <xsl:apply-templates/> <!-- apply-templates continues parsing down the tree, looking for more template matches. -->
        </ul>
    </xsl:template>
    
    <xsl:template match="Section">
        <li><xsl:value-of select="@Title"/></li>
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    
    <xsl:template match="Lesson">
        <li><xsl:value-of select="@Title"/></li>
    </xsl:template>
    

    【讨论】:

    • 谢谢,这让我很清楚,非常感谢! (对于像我这样的任何剪切贴图,@title 应该是 @Title 与上面的例子)
    猜你喜欢
    • 2021-05-04
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多