我有一些(格式错误的 DITA)XML 格式如下:
<dl>
<dlentry><dt>BLARG</dt>
<dd>BLARG Definition</dd>
<dt>BLARG2</dt>
<dd>BLARG2 Definition</dd></dlentry>
</dl>
<dl>
<dlentry><dt>BLARG3</dt>
<dd>BLARG3 Definition</dd></dlentry>
</dl>
<p>Continuation of BLARG3 definition.</p>
<note>Note pertaining to BLARG3 definition</note>
等等。
我想做的是将这一系列<dl>
元素合并为一个<dl>
,如下所示:
<dl>
<dlentry><dt>BLARG</dt>
<dd>BLARG Definition</dd></dlentry>
<dlentry><dt>BLARG2</dt>
<dd>BLARG2 Definition</dd></dlentry>
<dlentry><dt>BLARG3</dt>
<dd>BLARG3 definition. Continuation of BLARG3 definition.
<note>Note pertaining to BLARG3 definition</note></dd></dlentry>
<dl>
我正在尝试根据前面的值使用键来索引任何<p>
或<note>
节点,如下所示:generate-id()
<dd>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="kFollowing" match="p|note|table" use="generate-id(preceding::dd[1])"/>
<!-- identity template -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dl">
<xsl:choose>
<xsl:when test="count(preceding::dl)=0">
<!--We only want to process the first dl. All others should be ignored, because their content will get included in this one.-->
<dl>
<!--Want to grab all following terms, except ones that contain phrase elements, which need to be turned into a table because they are field values-->
<xsl:for-each select="following::dt[not(child::ph)]|descendant::dt">
<dlentry>
<dt><xsl:value-of select="normalize-space(.)"/></dt>
<xsl:variable name="ddID"><xsl:value-of select="generate-id(following::dd[1])"/></xsl:variable>
<dd><xsl:for-each select="following::dd[1]">
<xsl:apply-templates/><xsl:text>. </xsl:text>
</xsl:for-each>
<xsl:variable name="ddKey" select="key('kFollowing',generate-id(following::dd[1]))"/>
<xsl:for-each select="following::*[$ddKey]">
<xsl:apply-templates/>
</xsl:for-each>
</dd>
</dlentry>
</xsl:for-each>
</dl>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>