2011-12-27 84 views
0

说我有XMLise书完整参考 - Java。现在,我没有写这本书的内容,而是想从标签中生成它。所以..XSLT从XML文件中获取内容..?

以下是XML结构 -

<Books> 
    <Book> 
    <Part n="1" h="The Java Language"> 
    <SubHead h="Basics"> 
    <Topic n="1" h="The History and Evolution of Java"> 
    ..... 
    </Topic> 
    <Topic n="2" h="An overview of Java"> 
    ..... 
    </Topic> 
    <Topic n="3" h="Data Types, Variables, and Arrays"> 
    ..... 
    </Topic> 
    </SubHead> 
    <SubHead h="Intermediate"> 
    <Topic n="4" h="Operators"> 
    ..... 
    </Topic> 
    <Topic n="5" h="Control Statements"> 
    ..... 
    </Topic> 
    <Topic n="6" h="Looping"> 
    ..... 
    </Topic> 
    </SubHead> 
    </Part> 
    <Part n="2" h="OOPS"> 
    <SubHead h="Basics"> 
    <Topic n="7" h="Introduction to Classes"> 
    ..... 
    </Topic> 
    <Topic n="8" h="Inheritance"> 
    ..... 
    </Topic> 
    </SubHead> 
    <SubHead h="Intermediate"> 
    <Topic n="8" h="Packages and Interfaces"> 
    ..... 
    </Topic> 
    <Topic n="9" h="Exception Handling"> 
    ..... 
    </Topic> 
    </SubHead> 
    </Part> 
</Book> 
</Books> 

虚线是指书的内容。现在,如何获取HTML中的以下输出以及主题标签内容的详细说明。我的意思是说,我正在寻找任何书的内容部分。

Part 1 - The Java Language 
    Basics 
     1. The History and Evolution of Java 
     2. An overview of Java 
     3. Data Types, Variable, and Arrays 
    Intermediate 
     4. Operators 
     5. Control Statements 
     6. Looping 
Part 2 - OOPS 
    Basics 
     7. Introduction to Classes 
     8. Inheritance 
    Intermediate 
     9. Packages and Interfaces 
     10. Exception Handling 
+2

这是一门功课的问题吗?你试过什么了? – 2011-12-27 09:54:05

回答

1

继XSL给你一个输出数据你问:

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

    <xsl:template match="Book" > 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="Part" > 
     Part <xsl:value-of select="@n"/> - <xsl:value-of select="@h"/> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="SubHead"> 
     <xsl:value-of select="@h"/> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="Topic" > 
     <xsl:value-of select="@n"/>. <xsl:value-of select="@h"/> 
    </xsl:template> 
</xsl:stylesheet> 

输出将是:

Part 1 - The Java Language 
     Basics 
      1. The History and Evolution of Java 
      2. An overview of Java 
      3. Data Types, Variables, and Arrays 

     Intermediate 
      4. Operators 
      5. Control Statements 
      6. Looping 



Part 2 - OOPS 
     Basics 
      7. Introduction to Classes 
      8. Inheritance 

     Intermediate 
      8. Packages and Interfaces 
      9. Exception Handling 
+0

为了避免输出中不需要的空白,您只需要添加'xsl:strip-space elements =“*”'和/或'' – 2011-12-27 13:46:56