Xml 用XSL制作图书索引
我有一个具有以下结构的XML,其中我想对一些特定单词进行索引:Xml 用XSL制作图书索引,xml,xslt,Xml,Xslt,我有一个具有以下结构的XML,其中我想对一些特定单词进行索引: <book> <chapter title="This is first chapter"> <section title="This is the first section"> <paragraph title="This is the first paragraph">This is the paragraph content, where thi
<book>
<chapter title="This is first chapter">
<section title="This is the first section">
<paragraph title="This is the first paragraph">This is the paragraph content, where this <index>word</index> should be in the index</paragraph>
</section>
</chapter>
<chapter title="This is second chapter">
<section title="This is the first section">
<paragraph title="This is the first paragraph">This is the paragraph content</paragraph>
</section>
</chapter>
</book>
这是段落内容,这个词应该在索引中
这是段落内容
因此,我想列出所有
元素,下面是我尝试的内容:
<xsl:template match="/">
<xsl:apply-templates select="book/chapter" />
</xsl:template>
<xsl:template match="chapter">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>
Index
</h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="index">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
指数
指数
因此,所有单词都正确打印,但问题是XML中的所有文本都打印得一团糟(所有TextNode都是依次打印的)。我只需要索引元素,其他什么都不需要。获取所有文本节点的原因是 尝试添加模板:
<xsl:template match="text()"/>
获取所有文本节点的原因是
尝试添加模板:
<xsl:template match="text()"/>
事实上,我认为您需要更少的模板,而不是更多:
<xsl:template match="/book">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<xsl:apply-templates select="descendant::index"/>
</body>
</html>
</xsl:template>
<xsl:template match="index">
<p><xsl:value-of select="."/></p>
</xsl:template>
指数
指数
或者,如果您愿意:
<xsl:template match="/book">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<xsl:apply-templates select="descendant::index" >
<xsl:sort select="." data-type="text" order="ascending"/>
</xsl:apply-templates >
</body>
</html>
</xsl:template>
<xsl:template match="index">
<p><xsl:value-of select="."/></p>
</xsl:template>
指数
指数
事实上,我认为您需要更少的模板,而不是更多:
<xsl:template match="/book">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<xsl:apply-templates select="descendant::index"/>
</body>
</html>
</xsl:template>
<xsl:template match="index">
<p><xsl:value-of select="."/></p>
</xsl:template>
指数
指数
或者,如果您愿意:
<xsl:template match="/book">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<xsl:apply-templates select="descendant::index" >
<xsl:sort select="." data-type="text" order="ascending"/>
</xsl:apply-templates >
</body>
</html>
</xsl:template>
<xsl:template match="index">
<p><xsl:value-of select="."/></p>
</xsl:template>
指数
指数
使用适当的应用模板
选择,我将章节标题添加到索引标题中
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="book/chapter" />
</xsl:template>
<xsl:template match="chapter">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1><xsl:value-of select="@title"/> Index</h1>
<xsl:apply-templates select=".//index" />
</body>
</html>
</xsl:template>
<xsl:template match="text()">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
指数
指数
使用适当的应用模板
选择,我将章节标题添加到索引标题中
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="book/chapter" />
</xsl:template>
<xsl:template match="chapter">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1><xsl:value-of select="@title"/> Index</h1>
<xsl:apply-templates select=".//index" />
</body>
</html>
</xsl:template>
<xsl:template match="text()">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
指数
指数
这里是生成真实索引的粗略尝试,即分组、排序并包括每个条目所在位置的列表(以第#章、第#段的形式)
以下是一些假设:
- 您的处理器支持EXSLT set:distinct()函数(从而避免了对Muenchian分组的需要);
- 所有索引项都出现在段落元素中(尽管不一定是直接子项);每个索引条目在同一段落中仅出现一次;
- 所有段落都是一节的子段;所有章节都是一章的子章节。
指数
指数
-
.
,
这里是生成真实索引的粗略尝试,即分组、排序并包括每个条目所在位置的列表(以第#章、第#段的形式)
以下是一些假设:
- 您的处理器支持EXSLT set:distinct()函数(从而避免了对Muenchian分组的需要);
- 所有索引项都出现在段落元素中(尽管不一定是直接子项);每个索引条目在同一段落中仅出现一次;
- 所有段落都是一节的子段;所有章节都是一章的子章节。
指数
指数
-
.
,
您只需要按文档顺序列出所有索引单词的简单列表吗?不知道他们的位置?(或对重复项进行排序或分组)?获取所有文本节点的原因是XSLT内置规则()。尝试添加模板
@user3016153我会订购,但这可能是以后的另一个问题。@DanielHaley成功了!非常感谢你的洞察力!如果您愿意,可以将其添加为答案。您只需要按文档顺序列出所有索引单词的简单列表吗?不知道他们的位置?(或对重复项进行排序或分组)?获取所有文本节点的原因是XSLT内置规则()。尝试添加模板
@user3016153我会订购,但这可能是以后的另一个问题。@DanielHaley成功了!非常感谢你的洞察力!如果你愿意的话,把它作为一个答案添加进去。这实际上非常有效!谢谢你的分类。有没有一种简单的方法可以合并相似的元素,并为特定索引词的特定出现添加章节和段落位置?比如“word-2.5、3.1、4.2”。我想我应该提出一个新问题了?@janlindso你熟悉吗?这实际上非常有效!谢谢你的分类。有没有一种简单的方法可以合并相似的元素,并为特定索引词的特定出现添加章节和段落位置?比如“word-2.5、3.1、4.2”。“我想我应该问一个新问题了?”詹林德:你熟悉吗?