2011-12-27 83 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