Html 在XSLT中生成指向按字母顺序排列的文档部分的快捷链接
我正在编写一个样式表,将按字母顺序排列的XML图书索引转换为XHTML。作为这一过程的一部分,我必须:Html 在XSLT中生成指向按字母顺序排列的文档部分的快捷链接,html,xml,xslt,Html,Xml,Xslt,我正在编写一个样式表,将按字母顺序排列的XML图书索引转换为XHTML。作为这一过程的一部分,我必须: 将锚定标记添加到每个字母的第一个条目中 在顶部用字母表中的每个字母创建一行,每个字母都有指向正确定位点的链接 如果没有相应的锚点,则顶部的链接应不同。(例如,链接可能有一个额外的title属性。) 下面是一个简化的XML示例 <?xml version="1.0" encoding="UTF-8"?> <index> <heading>I'm an
<?xml version="1.0" encoding="UTF-8"?>
<index>
<heading>I'm an index</heading>
<primaryEntry>Andy</primaryEntry>
<primaryEntry>Bob</primaryEntry>
<primaryEntry>Jimmy</primaryEntry>
<primaryEntry>Johnny</primaryEntry>
<primaryEntry>Josephine</primaryEntry>
<primaryEntry>Zed</primaryEntry>
</index>
我是一个索引
安迪
上下快速移动
吉米
约翰尼
约瑟芬
泽德
这是预期的输出
<?xml version="1.0" encoding="UTF-8"?><html><head>Index</head><body>
<h1>I'm an index</h1>
<p><a href="#A">A</a> <a href="#B">B</a> <a href="#C" title="I go nowhere">C</a> <a href="#D" title="I go nowhere">D</a> ... <a href="#J">J</a> ... <a href="#Y" title="I go nowhere">Y</a> <a href="#Z">Z</a> </p>
<a name="A"></a><p>Andy</p>
<a name="B"></a><p>Bob</p>
<a name="J"></a><p>Jimmy</p>
<p>Johnny</p>
<p>Josephine</p>
<a name="Z"></a><p>Zed</p>
</body></html>
索引
我是一个索引
安迪
鲍勃
吉米
约翰尼
约瑟芬
泽德
这是我的尝试,不会成功。(见评论。)
指数
这种方法适用于需求1和需求2,但我不确定3是否可以使用这种方法。一旦我为每个节点计算一个节点集,就不会“弹出”来检查XML文档中是否有相应的元素。我是XSLT的新手,我强烈怀疑我是在倒退地思考整个问题,但我不知道从这里可以走到哪里 那么
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html" version="5.0" doctype-system="about:legacy-compat"
encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="letter" match="primaryEntry" use="substring(.,1,1)" />
<xsl:template match="/">
<xsl:variable name="main-doc" select="." />
<html>
<head>
<meta charset="utf-8" />
<title>Sample Corporation #1</title>
</head>
<body>
<h1>I'm an index</h1>
<p>
<xsl:for-each select="'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'">
<a href="#{.}">
<xsl:if test="not( key('letter',.,$main-doc))">
<xsl:attribute name="title">I go nowhere</xsl:attribute>
</xsl:if>
<xsl:value-of select="." />
</a>
</xsl:for-each>
</p>
<xsl:for-each-group select="index/primaryEntry" group-by="substring(.,1,1)">
<a name="{current-grouping-key()}"></a>
<xsl:apply-templates select="current-group()" />
</xsl:for-each-group>
</body>
</html>
</xsl:template>
<xsl:template match="primaryEntry">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
样本公司#1
我是一个索引
安迪
鲍勃
吉米
约翰尼
约瑟芬
泽德
“2.在字母表的顶部为每个字母创建一行,”为什么不只创建指向现有字母的链接?哈哈,这正是我的第一个冲动,但它必须与以前系统生成的内容相匹配。天知道管理风格的人在想什么。我可以试着说服他们改变它,这让我挺过来了。我在实验中使用key()时遇到了问题,但直到我意识到有三个参数的形式,我才真正理解为什么。将变量设置回外部上下文正是我所需要的。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html" version="5.0" doctype-system="about:legacy-compat"
encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="letter" match="primaryEntry" use="substring(.,1,1)" />
<xsl:template match="/">
<xsl:variable name="main-doc" select="." />
<html>
<head>
<meta charset="utf-8" />
<title>Sample Corporation #1</title>
</head>
<body>
<h1>I'm an index</h1>
<p>
<xsl:for-each select="'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'">
<a href="#{.}">
<xsl:if test="not( key('letter',.,$main-doc))">
<xsl:attribute name="title">I go nowhere</xsl:attribute>
</xsl:if>
<xsl:value-of select="." />
</a>
</xsl:for-each>
</p>
<xsl:for-each-group select="index/primaryEntry" group-by="substring(.,1,1)">
<a name="{current-grouping-key()}"></a>
<xsl:apply-templates select="current-group()" />
</xsl:for-each-group>
</body>
</html>
</xsl:template>
<xsl:template match="primaryEntry">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
<!DOCTYPE html SYSTEM "about:legacy-compat">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<title>Sample Corporation #1</title>
</head>
<body>
<h1>I'm an index</h1>
<p>
<a href="#A">A</a>
<a href="#B">B</a>
<a href="#C" title="I go nowhere">C</a>
<a href="#D" title="I go nowhere">D</a>
<a href="#E" title="I go nowhere">E</a>
<a href="#F" title="I go nowhere">F</a>
<a href="#G" title="I go nowhere">G</a>
<a href="#H" title="I go nowhere">H</a>
<a href="#I" title="I go nowhere">I</a>
<a href="#J">J</a>
<a href="#K" title="I go nowhere">K</a>
<a href="#L" title="I go nowhere">L</a>
<a href="#M" title="I go nowhere">M</a>
<a href="#N" title="I go nowhere">N</a>
<a href="#O" title="I go nowhere">O</a>
<a href="#P" title="I go nowhere">P</a>
<a href="#Q" title="I go nowhere">Q</a>
<a href="#R" title="I go nowhere">R</a>
<a href="#S" title="I go nowhere">S</a>
<a href="#T" title="I go nowhere">T</a>
<a href="#U" title="I go nowhere">U</a>
<a href="#V" title="I go nowhere">V</a>
<a href="#W" title="I go nowhere">W</a>
<a href="#X" title="I go nowhere">X</a>
<a href="#Y" title="I go nowhere">Y</a>
<a href="#Z">Z</a>
</p>
</a name="A">
<p>Andy</p>
</a name="B">
<p>Bob</p>
</a name="J">
<p>Jimmy</p>
<p>Johnny</p>
<p>Josephine</p>
</a name="Z">
<p>Zed</p>
</body>
</html>