xsl:number在跳过xsl:for-each迭代时递增。

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

xsl:number increments when xsl:for-each iteration is skipped

问题

我有XML消息:

<DataList>
    <Datainfo>					
    	<Name>Doc_image.jpg</Name>				
    	<Type>Image</Type>										
    </Datainfo>

	<Datainfo>					
		<Name>Spec_text1.txt</Name>				
		<Type>Document</Type>											
	</Datainfo>

	<Datainfo>					
		<Name>Spec_text2.txt</Name>				
		<Type>Document</Type>											
	</Datainfo>
</DataList>

它应该转换为另一个XML,满足以下两个条件:

  • 仅处理Type = 'Document' 的那些 <Datainfo> 块。
  • 总共不超过5个块。(我使用 xsl:number 作为计数器)

我的XSL如下:

<xsl:template match="//*:DataList">
<xsl:for-each select="./*:Datainfo[./*:Type = 'Document']">
	<xsl:variable name="num">
		<xsl:number/>
	</xsl:variable>

	<xsl:if test="$num <= 5">
		<xsl:element name="Name{$num}">
			<xsl:value-of select="./*:Name"/>
		</xsl:element>
	</xsl:if>
</xsl:for-each>
</xsl:template>

第一个Type为Image的块被跳过,但计数器仍考虑了此迭代并且仍然递增!现在我得到了这个结果:

<Name2>Spec_text1.txt</Name2>
<Name3>Spec_text2.txt</Name3>

期望的结果是:

<Name1>Spec_text1.txt</Name1>
<Name2>Spec_text2.txt</Name2>

我在这里陷入困境,因为据我所知,我无法创建自己的计数器并在需要时进行增加/减少,就像在Java中一样。任何帮助将不胜感激。

英文:

I have the XML message:

<DataList>
    <Datainfo>					
    	<Name>Doc_image.jpg</Name>				
    	<Type>Image</Type>										
    </Datainfo>

	<Datainfo>					
		<Name>Spec_text1.txt</Name>				
		<Type>Document</Type>											
	</Datainfo>

	<Datainfo>					
		<Name>Spec_text2.txt</Name>				
		<Type>Document</Type>											
	</Datainfo>
</DataList>

Which should be transformed to another XML with 2 requirements:

  • Only those <Datainfo> blocks where Type = 'Document'.
  • Not more that 5 blocks in total. (For that purpose I used xsl:number as counter)

My XSL is:

<xsl:template match="//*:DataList">
<xsl:for-each select="./*:Datainfo[./*:Type = 'Document']">
	<xsl:variable name="num">
		<xsl:number/>
	</xsl:variable>

	<xsl:if test="$num <= 5">
		<xsl:element name="Name{$num}">
			<xsl:value-of select="./*:Name"/>
		</xsl:element>
	</xsl:if>
</xsl:for-each>
</xsl:template>

The first block with Type Image is skipped, but counter still considers this iteration and increments anyway! Now I get this:

<Name2>Spec_text1.txt</Name2>
<Name3>Spec_text2.txt</Name3>

The expected result is:

<Name1>Spec_text1.txt</Name1>
<Name2>Spec_text2.txt</Name2>

I got stuck here, because as I understand I can't create my own counter and increment/decrement it whenever I want, like in Java.
Any help appreciated.

答案1

得分: 2

只需使用 <xsl:for-each select="./*:Datainfo[./*:Type = 'Document'][position() le 5]"><xsl:element name="Name{position()}">

英文:

Simply use <xsl:for-each select="./*:Datainfo[./*:Type = 'Document'][position() le 5]"> and <xsl:element name="Name{position()}">.

huangapple
  • 本文由 发表于 2020年10月26日 21:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64538363.html
匿名

发表评论

匿名网友

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

确定