我想要一种方式来格式化我的学习笔记,采取一个输入文件,并输出一个干净的,HTML文件。今天,我教会了自己基本的XML和XSTL (并且对HTML和CSS有一定的知识)来完成这个任务。因此,我将有一个带有注释内容的简单XML文件,如下所示:
<?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文档应该将我的注释划分为一个任意数量的部分,每个部分都有一个子标题,以及任意数量的点点和点点,它们的代码块格式不同。
然后,要格式化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元素。
不过,我有些问题:
-I没有一种让点点具有不同程度的缩进的方法。因此,我不能有一个点点,上面写着“类”,然后在那个点下面有几个点点,稍微缩进一点,讨论一个类在一点上是什么,在另一点上命名约定,等等。我想这个解决方案可能与创建一些新的XML元素有关,比如point2,它将被格式化为:
- POINT
而不是
(导致缩进的额外HTML元素)。
-If,在我的XML文档中,我按特定的顺序定义各种点点和代码块(例如,点、codeBlock、点),在XSLT样式表完成其工作后生成的HTML文件将所有的点集合在一起,然后是所有的codeBlocks (例如,point,codeBlock,codeBlock)。我理解为什么会发生这种情况;我的XSLT文档遍历所有XML元素,然后遍历所有XML元素。我猜解决方案将与循环遍历元素的所有子节点有关,如果下一个元素是元素,则输出一个元素,如果下一个元素是元素,则输出另一个元素。
有什么想法吗?可以随意建议对XML或XSLT文档进行结构调整。也许我应该使用属性进行缩进,或者区分点和codeBlocks?
谢谢你!!
发布于 2013-03-21 13:14:51
您正在经历的群集是避免使用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和ULs:
<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>https://stackoverflow.com/questions/15548224
复制相似问题