我对xslt相当陌生。所以我想要做的是用xml将一本书解析为html。一个基本的例子就是这个。
<book>
<title>
Some important title
</title>
<section>
<title>animal</title>
<kw>RealAnimal</kw>
<kw>something|something more about it</tkw>
<para>Some really important facts</para>
<section>
<title>something</title>
<kw>something else</kw>
<para>Enter Text</para>
</section>
<section>
<title>Even more</title>
<kw>and more</kw>
<para>hell of a lot more</para>
</section>
</section>
</book>一个区段可以有一个未知数目的子节。所以很明显我需要用复述来处理这件事。到目前为止,我设计了两个模板,以便根据我的需要处理一本书和一个章节。
<xsl:template match="book">
<html>
<body>
<h1><xsl:value-of select="title" /></h1>
<xsl:apply-templates select="section" />
</body>
</html>
</xsl:template>
<xsl:template match="section[title]">
<li><xsl:value-of select="title" /></li>
<!-- do something more here -->
</xsl:template>我不知道的是,我能不能得到我当前的递归深度,因为我想根据深度来决定使用哪种标题。
另外,这本书应该由两部分组成。它的正常内容在开始,如标题和段以下的标题。最后还有一个指数。这让我相信,我需要在一个文档中以两种不同的方式解析它,但我将如何做到这一点?任何提示或代码将不胜感激
所以我想出了如何用数字来制作章节和分节的标题,就像Word中的列表。
<xsl:number level="multiple" />给我一个x.y小节的基础上,父母部门的立场和它自己的立场。我现在想要的是,它给了我分组的数量,因为它根据深度对值进行分组,但我不知道如何进行分组。
我所期望的是它分析到
<h1>Some important title</h1>
...
<h2> animal </h2>
...
<h3> something </h3>
...
<h3> Even more </h3>如果我要在“某某物”( the )-section中添加另一个部分,它将是h4等等。
就这样解决了
<xsl:param name="depth"/>
<xsl:choose>
<xsl:when test="6 > $depth">
<xsl:element name="h{$depth}">
<xsl:number level="multiple" />.
<xsl:value-of select="title" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<h6><xsl:number level="multiple" />. <xsl:value-of select="title" /></h6>
</xsl:otherwise>
</xsl:choose>发布于 2014-07-02 21:07:19
我想要做的是把h2设置为图书的分段,h3设置为区段的分段
这里有一种方法可以做到这一点--不受限制地嵌套分段:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/book">
<html>
<body>
<h1><xsl:value-of select="title" /></h1>
<xsl:apply-templates select="section" />
</body>
</html>
</xsl:template>
<xsl:template match="section">
<h2><xsl:value-of select="title" /></h2>
<xsl:apply-templates select="subsection">
<xsl:with-param name="depth" select="3"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="subsection">
<xsl:param name="depth"/>
<xsl:element name="h{$depth}">
<xsl:value-of select="title" />
</xsl:element>
<xsl:apply-templates select="subsection">
<xsl:with-param name="depth" select="$depth + 1"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>注意,这个是递归,是无限的;AFAIK,HTML在h6之后将耗尽级别。
编辑:
一个分段不是命名为分段,而是作为另一个部分的一个子部分命名的。
那这就更简单了。或者至少更短。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/book">
<html>
<body>
<h1><xsl:value-of select="title" /></h1>
<xsl:apply-templates select="section">
<xsl:with-param name="depth" select="2"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="section">
<xsl:param name="depth"/>
<xsl:element name="h{$depth}">
<xsl:value-of select="title" />
</xsl:element>
<xsl:apply-templates select="section">
<xsl:with-param name="depth" select="$depth + 1"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>编辑2:
我应该为前4设置h2-h5,然后设置h6。
如果您的意思是希望将标题限制在h6的最大值,而不管该节的深度如何,那么请更改以下内容:
<xsl:with-param name="depth" select="$depth + 1"/>至:
<xsl:with-param name="depth" select="$depth + ($depth < 6)"/>发布于 2014-07-02 18:49:45
如果你真的想知道你有多深,你可以试着修改一下“count(祖先::*)”。不过,我建议先看一下自动编号,以防它起作用。它甚至非常轻松地处理嵌套项。
“XSLT的xsl:number指令使将一个数字插入到结果文档中很容易。它的value属性允许您命名要插入的数字,但是如果您真的希望将特定的数字添加到结果中,则将该数字添加为文字文本要简单得多。当您从xsl: value --指令中省略value属性时,XSLT处理器将根据上下文节点在源树中的位置或由xsl: for -每条指令计数的节点中的节点计算出数字,这对自动编号非常有用。”
XML.com参考页
https://stackoverflow.com/questions/24538713
复制相似问题