0

我有一组文件,其中包含要编译成单个列表的定义。文件列表存储在一个 XML 文件中,如下所示(这是输入文件):

 <report>
 <incident>
 <file>Balance_fields_selected.htm</file>
 </incident>
 <incident>
 <file>Cd_fields.htm</file>
 </incident>
 </report>

一个元素指定的每个文件都<file>包含一系列<p class='Term'>我需要编译成单个列表的元素。其中每一个后面都是需要分组的任意数量的其他元素(我正在尝试使用键):

 <html><body>
 <p class="Term">
<a name="Accrued_Bonus_Interest" id="Accrued_Bonus_Interest"></a>Accrued (Bonus Interest)</p>
<p>Bonus Interest Accrued Cycle-to-Date. &#160;Amount of bonus interest that has accrued on the time deposit.</p>
<p>Pages: &#160;View CD Detail.</p>
<p class="Term">
<a name="Accrued_OID" id="Accrued_OID"></a>Accrued (Original Issue Discount)</p>
<p>Original Issue Discount Interest Accrued Year-to-Date. &#160;OID interest accrued in the current year.</p>
<p>Pages: &#160;View CD Detail.</p> 
 </body></html>

期望的结果类似于:

 <topic>
 <title>Arbitrary title</title>
 <body>
 <dl>
 <dlentry id="Accrued_Bonus_Interest">
 <dt>Accrued (Bonus Interest)</dt>
 <dd><p>Bonus Interest Accrued Cycle-to-Date. &#160;Amount of bonus interest that has accrued on the time deposit.</p>
 <p>Pages: View CD Detail</p></dd></dlentry>
 <dlentry id="Accrued_OID">
 <dt>Accrued (Original Issue Amount)</dt>
 <dd><p>Original Issue Discount Interest Accrued Year-to-Date. &#160;OID interest accrued in the current year.</p>
 <p>Pages: View CD Detail</p></dd>
 </dl>
 </body>
 </topic>

我有一个样式表已经完成了大部分工作——当谈到正确使用键时,我似乎只是(再次)迷失了方向。以下样式表:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:strip-space elements="*" />

<xsl:key name="kFollowing" match="*[not(p[@class='Term'])]" 
    use="generate-id(preceding::p[@class='Term'][1])"/>

<xsl:template match="/">
    <![CDATA[ 
    <!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">  ]]>
    <topic id="data_dictionary">
        <title>IBS Insight Data Dictionary</title>
        <body>
    <dl>
        <xsl:for-each select="/report/incident/file">
            <xsl:for-each select="document(.)/descendant::p[@class='Term']">
                <xsl:variable name="vFollowing" select="key('kFollowing', generate-id())"/> 
                <xsl:element name="dlentry">
                    <xsl:attribute name="id">
                        <xsl:value-of select="child::a/@id"/>
                    </xsl:attribute>
                    <dt><xsl:value-of select="."/></dt>
                    <dd><xsl:value-of select="$vFollowing"/>
                        <xsl:for-each select="following::*[$vFollowing]">
                            <xsl:apply-templates select="."/>
                        </xsl:for-each>
                    </dd>
                </xsl:element>
            </xsl:for-each>
      </xsl:for-each>                
    </dl></body></topic>
</xsl:template>

</xsl:stylesheet>

正确抓取输入文件中定义的文件,并生成dl具有正确dlentryid 和dt元素的文件。问题是我对键的混淆实现是组装dd元素的方式。我的示例 xml 在这里并不明显,但正在发生的事情是每个<p class='Term'>都在抓取所有后续的 contnet 来填充它<dd>,如下所示:

     <topic>
 <title>Arbitrary title</title>
 <body>
 <dl>
 <dlentry id="Accrued_Bonus_Interest">
 <dt>Accrued (Bonus Interest)</dt>
 <dd>Bonus Interest Accrued Cycle-to-Date. &#160;Amount of bonus interest that has accrued on the time deposit. Pages: View CD DetailAccrued (Original Issue Amount)Original Issue Discount Interest Accrued Year-to-Date. &#160;OID interest accrued in the current year.Pages: View CD Detail</dd></dlentry>
 <dlentry id="Accrued_OID">
 <dt>Accrued (Original Issue Amount)</dt>
 <dd><p>Original Issue Discount Interest Accrued Year-to-Date. &#160;OID interest accrued in the current year.</p>
 <p>Pages: View CD Detail</p></dd>
 </dl>
 </body>
 </topic>

每个文件中的最后一项都正确呈现,但这只是因为没有更多的后续节点要处理。关于我的代码的某些内容是与键匹配的节点太多。

感谢您的关注。

4

1 回答 1

2

我会稍微更改键定义:

<xsl:key name="kFollowing"
    match="*[not(self::p[@class='Term'])][preceding-sibling::p[@class='Term']" 
    use="generate-id(preceding-sibling::p[@class='Term'][1])"/>

这匹配任何本身不是 a<p class="Term">但在树中与这样的元素处于同一级别的元素,并按它们最近的前面“术语”对它们进行分组。如果您想允许紧随其后的内容<p class="Term">只是文本节点(即不在任何元素内)的情况,那么您需要

<xsl:key name="kFollowing"
    match="node()[not(self::p[@class='Term'])][preceding-sibling::p[@class='Term']" 
    use="generate-id(preceding-sibling::p[@class='Term'][1])"/>

然后您可以将内部 for-each 简化为

        <xsl:for-each select="document(.)/descendant::p[@class='Term']">
            <dlentry id="{a/@id}">
                <dt><xsl:value-of select="."/></dt>
                <dd><xsl:copy-of select="key('kFollowing', generate-id())"/></dd>
            </dlentry>
        </xsl:for-each>
于 2012-11-19T22:42:06.557 回答