2013-03-21 28 views
0

我想要一种格式化我的学习笔记的方法,即获取输入文件并输出干净的HTML文件。我今天自学了基本的XML和XSTL(并且具有HTML和CSS的先前知识)来完成此任务。所以我会用音符内容的简单的XML文件,如下所示:使用XML和XSLT格式化我的学习笔记

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> 
<root> 
    <heading>Programming Fundamentals 48023</heading> 
    <section> 
    <subheading>Classes and Objects</subheading> 
    <point>Something about classes</point> 
    <point>Some else about classes</point> 
    <codeBlock> 
     <text>How to create an instance of a class</text> 
     <code>Class class = new Class();</code> 
    </codeBlock> 
    <point>Something about objects</point> 
    . . . 
    </section> 
    <section> 
    <subheading>Methods</subheading> 
    <point>Something about methods</point> 
    <codeBlock> 
     <text>How to define a method</text> 
     <code>modifiers returnType methodName() { . . . }</code> 
    </codeBlock> 
    . . . 
    </section> 
    . . . 
</root> 

XML文档要分我的笔记到abitrary数量的部分,每一个副标题,和点任意数量的点和具有不同格式的代码块的点点。

然后,格式化XML文档,有一个XSLT样式表(用HTML和CSS等),看起来有点像这样:

<?xsl version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
    <head> 
    <title><xsl:value-of select="root/heading" /></title> 
    <style> 
     body {font-family:Tahoma; } 
     div {width:800px; margin-left:auto; margin-right:auto; } 
     h1 {color:#6FA5FD; border-bottom: 1px dotted #333333; text-indent:64px; } 
     h2 {color:#6FA5FD; border-bottom: 1px dotted #333333; text-indent:96px; } 
     pre {width:640px; border-style:dotted; border-width:1px; border-color:#333333; background-color:#CCCCFF; } 
     ul {list-style-type:square; color:#6FA5FD; } 
     li span {color:#000000; font-size:10; } 
    </style> 
    </head> 
    <body> 
    <div> 
<!-- - - - - - - - - CONTENT STARTS HERE - - - - - - - - --> 

<h1><xsl:value-of select="root/heading" /></h1> 
<xsl:for-each select="root/section"> 
    <h2><xsl:value-of select="subheading" /></h2> 
    <xsl:for-each select="point"> 
     <ul> 
     <li><span> 
      <xsl:value-of select="." /> 
     </span></li> 
     </ul> 
    </xsl:for-each> 
    <xsl:for-each select="codeBlock"> 
     <ul> 
     <li><span> <xsl:value-of select="./text" /> 
      <pre> <xsl:value-of select="./code" /> </pre> 
     </span></li> 
     </ul> 
    </xsl:for-each> 
</xsl:for-each> 

<!-- - - - - - - - - CONTENT ENDS HERE - - - - - - - - --> 
    </div> 
    </body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

所以XML元素成为一个格式化的HTML元素,在XML元素变成格式化的HTML元素。这很棒。然后,XML标签对点点和代码块进行分组,并且点点简单地变成带有蓝色项目符号和黑色文本的列表项目,并且代码块被格式化为HTML元素。

我遇到了一些问题,但:
- 我没有办法让点点具有不同级别的缩进。所以我不能有一个点说“Classes”,然后在这个下面有几个点点,稍微缩进,说一个类在一个点上是什么,在另一个点上命名约定,等我猜的解决方案必须是与做一些新的XML元素,如点2,这将被格式化为:

    • POINT

而不是
  • POINT

(引起缩进的额外HTML元素)。
- 如果在我的XML文档中,我按特定顺序(例如,point,point,codeBlock,point,codeBlock,point)定义各个点和代码块,则在XSLT样式表之后生成的HTML文件执行其工作将所有点集合在一起,然后将所有的codeBlocks(比如说,point,point,point,point,codeBlock,codeBlock)代替。我明白为什么会发生这种情况。我的XSLT文档遍历所有XML元素,然后遍历所有XML元素。我想这个解决方案可能与循环元素的所有子节点有关,如果下一个元素是元素,则输出一个,如果下一个元素是元素,则输出另一个元素。

任何想法?随意建议重新构建XML或XSLT文档。也许我应该使用属性进行缩进或区分点和codeBlocks?

谢谢!

回答

0

的群聚您遇到是一个很好的理由,以避免for-each和使用的模板,如以下:

<?xsl version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*" /> 
    <xsl:output indent="yes" omit-xml-declaration="yes" /> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     <title> 
      <xsl:value-of select="root/heading" /> 
     </title> 
     <style> 
      body {font-family:Tahoma; } 
      div {width:800px; margin-left:auto; margin-right:auto; } 
      h1 {color:#6FA5FD; border-bottom: 1px dotted #333333; text-indent:64px; } 
      h2 {color:#6FA5FD; border-bottom: 1px dotted #333333; text-indent:96px; } 
      pre {width:640px; border-style:dotted; border-width:1px; 
       border-color:#333333; background-color:#CCCCFF; } 
      ul {list-style-type:square; color:#6FA5FD; } 
      li span {color:#000000; font-size:10; } 
     </style> 
     </head> 
     <body> 
     <div> 
      <!-- - - - - - - - - CONTENT STARTS HERE - - - - - - - - --> 

      <h1> 
      <xsl:value-of select="root/heading" /> 
      </h1> 
      <xsl:apply-templates select="root/section" /> 

      <!-- - - - - - - - - CONTENT ENDS HERE - - - - - - - - --> 
     </div> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="section"> 
    <h2> 
     <xsl:value-of select="subheading" /> 
    </h2> 

    <xsl:apply-templates select="." mode="list" /> 
    </xsl:template> 

    <xsl:template match="*[point or codeBlock]" mode="list"> 
    <ul> 
     <xsl:apply-templates select="point | codeBlock" /> 
    </ul> 
    </xsl:template> 
    <xsl:template match="*" mode="list" /> 

    <xsl:template match="point"> 
    <li> 
     <span> 
     <xsl:apply-templates select="text()" /> 
     </span> 
     <xsl:apply-templates select="." mode="list" /> 
    </li> 
    </xsl:template> 

    <xsl:template match="codeBlock"> 
    <li> 
     <span> 
     <xsl:value-of select ="text" /> 
     <pre> 
      <xsl:value-of select ="code" /> 
     </pre> 
     </span> 
    </li> 
    </xsl:template> 
</xsl:stylesheet> 

我还包含上述变化处理多级控制点,这样你就可以做到这一点:

<point> 
    I have a point. 
    <point> 
    This is a sub point 
    <point>With a sub-sub point beneath it</point> 
    </point> 
    <point>This is just a sub point</point> 
</point> 

和生成的XML将嵌套LIS和上行线路:

<li> 
    <span> 
    I have a point. 
    </span> 
    <ul> 
    <li> 
     <span> 
     This is a sub point 
     </span> 
     <ul> 
     <li> 
      <span>With a sub-sub point beneath it</span> 
     </li> 
     </ul> 
    </li> 
    <li> 
     <span>This is just a sub point</span> 
    </li> 
    </ul> 
</li> 
+0

我很敬畏......它一切正常!感谢您的及时回复!我现在正在阅读它并试图理解它 - 我也将看一些模板教程。再次感谢! – Luke 2013-03-21 13:29:34