xsl:for-each 和 xsl:sort 重复使用相同的列表

huangapple go评论68阅读模式
英文:

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 &lt;dl&gt;. The XML is tagged like this. I assigned "sort" to the outputclass attribute to indicate this specific &lt;dl&gt; is to be sorted.

&lt;dl outputclass=&quot;sort&quot;&gt; 
		&lt;dlentry&gt; 
		  &lt;dt&gt; Ant 
		  &lt;/dt&gt; 
		  &lt;dd&gt; 
			 &lt;p&gt; A Java-based software tool for automating builds. 
			 &lt;/p&gt; 
		  &lt;/dd&gt; 
		&lt;/dlentry&gt; 
		&lt;dlentry&gt; 
		  &lt;dt&gt; Node 
		  &lt;/dt&gt; 
		  &lt;dd&gt; 
			 &lt;p&gt; A level in a document hierarchy. 
			 &lt;/p&gt; 
		  &lt;/dd&gt; 
		&lt;/dlentry&gt;
		&lt;dlentry&gt; 
		  &lt;dt&gt; DITA (Darwin Information Typing Architecture) 
		  &lt;/dt&gt; 
		  &lt;dd&gt; 
			 &lt;p&gt; An XML-based authoring model based on inheritance and specialization. 
			 &lt;/p&gt; 
		  &lt;/dd&gt; 
		&lt;/dlentry&gt; 
....
&lt;/dl&gt;

I know that &lt;xsl:sort&gt; needs to be nested in a &lt;xsl:for-each&gt;. After several attempts, this is the XSLT that has consistently generated sorted text for a PDF:

&lt;xsl:for-each select=&quot;../dlentry&quot;&gt;
   &lt;xsl:sort select=&quot;dt&quot;/&gt;
   &lt;fo:block xsl:use-attribute-sets=&quot;dlentry.dd__content&quot; margin-left=&quot;0pt&quot;&gt;
      &lt;xsl:apply-templates select=&quot;dt&quot;/&gt;
      &lt;xsl:apply-templates select=&quot;dd&quot;/&gt;
   &lt;/fo:block&gt;
&lt;/xsl:for-each&gt;

The problem is that it repeats the sorted list for each &lt;dlentry&gt; 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 &lt;dlentry&gt;? 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:

&lt;xsl:for-each select=&quot;../dlentry&quot;&gt;

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:

&lt;xsl:template match=&quot;dl&quot;&gt;
	&lt;xsl:for-each select=&quot;dlentry&quot;&gt;
	   &lt;xsl:sort select=&quot;dt&quot;/&gt;
	   &lt;fo:block xsl:use-attribute-sets=&quot;dlentry.dd__content&quot; margin-left=&quot;0pt&quot;&gt;
		  &lt;xsl:apply-templates select=&quot;dt&quot;/&gt;
		  &lt;xsl:apply-templates select=&quot;dd&quot;/&gt;
	   &lt;/fo:block&gt;
	&lt;/xsl:for-each&gt;
&lt;/xsl:template&gt; 

huangapple
  • 本文由 发表于 2023年8月4日 01:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定