我构建了一个XML工作流来创建一个富HTML文档,其中包含使用唯一ID的自动超链接和锚点。但是我需要为未标记的现有元素添加唯一的锚点。我不想通过数百个元素标记内容,所以我想知道是否有办法拉出开始一个段落的字母,然后将其附加到一个节号以创建唯一的ID锚属性。 XML看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<RULES>
<RULE>
<RULE_SECTION>
<RULE_subhead><num>3-3</num>. DOUBT AS TO PROCEDURE </RULE_subhead>
<rule_letterhead>a. Procedure </rule_letterhead>
<rule_letterhead-B>b. Determination of Score for Hole </rule_letterhead-B>
</RULE_SECTION>
<RULE_SECTION>
<RULE_subhead><num>4-1</num>. FORM AND MAKE OF CLUBS </RULE_subhead>
<rule_letterhead>a. General </rule_letterhead>
<rule_letterhead-B>b. Wear and Alteration </rule_letterhead-B>
<rule_letterhead-B>c. Damage in Normal Course of Play </rule_letterhead-B>
</RULE_SECTION>
</RULE>
</RULES>
请注意两个<rule_letterhead>
和<rule_letterhead-B>
元素如何以首字母和句点(a.b.c。等)开头。我想构建HTML看起来像这样:
<body>
<h2 class="RULE_subhead" id="r3-3">3-3. DOUBT AS TO PROCEDURE </h2>
<h3 class="rule_letterhead" id="r3-3a">a. Procedure </h3>
<h3 class="rule_letterhead" id="r3-3b">b. Determination of Score for Hole </h3>
<h2 class="RULE_subhead" id="r4-1">4-1. FORM AND MAKE OF CLUBS </h2>
<h3 class="rule_letterhead" id="r4-1a">a. General </h3>
<h3 class="rule_letterhead" id="r4-1b">b. Wear and Alteration </h3>
<h3 class="rule_letterhead" id="r4-1c">c. Damage in Normal Course of Play </h3>
</body>
因此,XSL必须提取Section编号并将其附加到元素<letterhead>
或<letterhead-B>
的第一个字母,如“4-1a”。
这是我目前正在使用的XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Untitled Document</title>
</head>
<body>
<xsl:apply-templates select="RULES"/>
</body>
</html>
</xsl:template>
<xsl:template match="RULE_subhead">
<h2 class="RULE_subhead" id="{concat('r',translate(normalize-space(num), ' ', ''))}">
<xsl:apply-templates/></h2>
</xsl:template>
<xsl:template match="rule_letterhead">
<h3 class="rule_letterhead"><xsl:apply-templates/></h3>
</xsl:template>
<xsl:template match="rule_letterhead-B">
<h3 class="rule_letterhead"><xsl:apply-templates/></h3>
</xsl:template>
</xsl:stylesheet>
它适用于所有其他元素。
由于
答案 0 :(得分:1)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" encoding="UTF-8"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Untitled Document</title>
</head>
<body>
<xsl:apply-templates select="RULES"/>
</body>
</html>
</xsl:template>
<xsl:template match="RULE_subhead">
<h2 class="RULE_subhead"
id="{concat('r',translate(normalize-space(num), ' ', ''))}">
<xsl:apply-templates/>
</h2>
</xsl:template>
<xsl:template match="rule_letterhead|rule_letterhead-B">
<h3 class="rule_letterhead"
id="{concat('r',translate(normalize-space(preceding-sibling::RULE_subhead/num), ' ', ''),
substring(., 1, 1))}">
<xsl:apply-templates/>
</h3>
</xsl:template>
</xsl:stylesheet>