英文:
xsl:for-each and xsl:sort repeat the same list
问题
我试图对<dl>中的项目列表进行排序。 XML标记如下。我将“sort”分配给outputclass属性,以指示要对此特定的<dl>进行排序。
<dl outputclass="sort">
<dlentry>
<dt> Ant
</dt>
<dd>
<p> A Java-based software tool for automating builds.
</p>
</dd>
</dlentry>
<dlentry>
<dt> Node
</dt>
<dd>
<p> A level in a document hierarchy.
</p>
</dd>
</dlentry>
<dlentry>
<dt> DITA (Darwin Information Typing Architecture)
</dt>
<dd>
<p> An XML-based authoring model based on inheritance and specialization.
</p>
</dd>
</dlentry>
....
</dl>
我知道<xsl:sort>需要嵌套在<xsl:for-each>中。经过多次尝试,这是一直为PDF生成排序文本的XSLT:
<xsl:for-each select="../dlentry">
<xsl:sort select="dt"/>
<fo:block xsl:use-attribute-sets="dlentry.dd__content" margin-left="0pt">
<xsl:apply-templates select="dt"/>
<xsl:apply-templates select="dd"/>
</fo:block>
</xsl:for-each>
问题是它会重复生成排序列表,每个<dlentry>在我的列表中都会生成一次:
Ant
A Java-based software tool for automating builds.
DITA (Darwin Information Typing Architecture)
An XML-based authoring model based on inheritance and specialization.
Node
A level in a document hierarchy.
Ant
A Java-based software tool for automating builds.
DITA (Darwin Information Typing Architecture)
An XML-based authoring model based on inheritance and specialization.
Node
A level in a document hierarchy.
Ant
A Java-based software tool for automating builds.
DITA (Darwin Information Typing Architecture)
An XML-based authoring model based on inheritance and specialization.
Node
A level in a document hierarchy.
如何获得排序的列表,并且只生成一次,而不是每个<dlentry>?谢谢您的帮助。
英文:
I'm trying to sort a list of items in a <dl>. The XML is tagged like this. I assigned "sort" to the outputclass attribute to indicate this specific <dl> is to be sorted.
<dl outputclass="sort">
<dlentry>
<dt> Ant
</dt>
<dd>
<p> A Java-based software tool for automating builds.
</p>
</dd>
</dlentry>
<dlentry>
<dt> Node
</dt>
<dd>
<p> A level in a document hierarchy.
</p>
</dd>
</dlentry>
<dlentry>
<dt> DITA (Darwin Information Typing Architecture)
</dt>
<dd>
<p> An XML-based authoring model based on inheritance and specialization.
</p>
</dd>
</dlentry>
....
</dl>
I know that <xsl:sort> needs to be nested in a <xsl:for-each>. After several attempts, this is the XSLT that has consistently generated sorted text for a PDF:
<xsl:for-each select="../dlentry">
<xsl:sort select="dt"/>
<fo:block xsl:use-attribute-sets="dlentry.dd__content" margin-left="0pt">
<xsl:apply-templates select="dt"/>
<xsl:apply-templates select="dd"/>
</fo:block>
</xsl:for-each>
The problem is that it repeats the sorted list for each <dlentry> in my list:
>Ant<br/>
>A Java-based software tool for automating builds.<br/>
>DITA (Darwin Information Typing Architecture)<br/>
>An XML-based authoring model based on inheritance and specialization.<br/>
>Node<br/>
>A level in a document hierarchy.<br/>
>Ant<br/>
>A Java-based software tool for automating builds.<br/>
>DITA (Darwin Information Typing Architecture)<br/>
>An XML-based authoring model based on inheritance and specialization.<br/>
>Node<br/>
>A level in a document hierarchy.<br/>
>Ant<br/>
>A Java-based software tool for automating builds.<br/>
>DITA (Darwin Information Typing Architecture)<br/>
>An XML-based authoring model based on inheritance and specialization.<br/>
>Node<br/>
>A level in a document hierarchy.<br/>
How do I get a sorted list, and only have it generated once instead of for each <dlentry>? Thank you for your help.
答案1
得分: 2
您只展示了代码的一部分,所以我们只能猜测。
不过,根据您的指令:
<xsl:for-each select="../dlentry">
只有在从dlentry
上下文中调用时才能工作,并且如果是这样调用,它将执行所有dlentry
元素的代码,不仅是当前的元素,还包括所有它的兄弟元素。
显然,您对每个dlentry
都这样做,所以最终代码会执行3x3次。
相反,您应该像这样做:
<xsl:template match="dl">
<xsl:for-each select="dlentry">
<xsl:sort select="dt"/>
<fo:block xsl:use-attribute-sets="dlentry.dd__content" margin-left="0pt">
<xsl:apply-templates select="dt"/>
<xsl:apply-templates select="dd"/>
</fo:block>
</xsl:for-each>
</xsl:template>
英文:
You are only showing us a part of your code, so we can only guess.
Still, your instruction:
<xsl:for-each select="../dlentry">
can work only if it is called from the context of dlentry
, and if so called, will execute the code for all dlentry
elements, not only the current one, but also all its siblings.
Apparently you are doing this for every dlentry
, so in the end the code is executed 3x3 times.
Instead you should be doing something like:
<xsl:template match="dl">
<xsl:for-each select="dlentry">
<xsl:sort select="dt"/>
<fo:block xsl:use-attribute-sets="dlentry.dd__content" margin-left="0pt">
<xsl:apply-templates select="dt"/>
<xsl:apply-templates select="dd"/>
</fo:block>
</xsl:for-each>
</xsl:template>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论