我有一個非常棘手的XSLT問題。 想象我有以下輸入:每個循環的XSLT
<OtherCharges>
<LineId>
<Id>P</Id>
</LineId>
<Items>
<Item1>1</Item1>
<Item2>2</Item2>
<Item3>3</Item3>
</Items>
<LineId>
<Id>P</Id>
</LineId>
<Items>
<Item1>4</Item1>
</Items>
<LineId>
<Id>P</Id>
</LineId>
<Items>
<Item1>5</Item1>
<Item2>6</Item2>
</Items>
</OtherCharges>
,並作爲輸出我想有這樣的:
<OtherCharges>
<LineId>P</LineId>
<OtherChargesValues>
<value>1</value>
<value>2</value>
<value>3</value>
</OtherChargesValues>
</OtherCharges>
<OtherCharges>
<LineId>P</LineId>
<OtherChargesValues>
<value>4</value>
</OtherChargesValues>
</OtherCharges>
<OtherCharges>
<LineId>P</LineId>
<OtherChargesValues>
<value>5</value>
<value>6</value>
</OtherChargesValues>
</OtherCharges>
我在哪裏可以有線條的數量不受限制,但每行最多3項。 我試過下面的代碼:
<xsl:for-each select="/OtherCharges/LineId">
<Id>
<xsl:value-of select="Id"/>
<Id>
<xsl:variable name="ChargeLine" select="."/>
<xsl:for-each select="following-sibling::Items[preceding-sibling::LineId[1] = $ChargeLine]">
<xsl:if test="name(.)='Items'">
<xsl:if test="Item1">
<value>
<xsl:value-of select="Item1"/>
</value>
</xsl:if>
<xsl:if test="Item2">
<value>
<xsl:value-of select="Item2"/>
</value>
</xsl:if>
<xsl:if test="Item3">
<value>
<xsl:value-of select="Item3"/>
</value>
</xsl:if>
</xsl:if>
</xsl:for-each>
如果ID的是不同的效果很好,但問題是,當我有相同的ID值(如在本例中)。 任何人都可以幫助我嗎?
謝謝。
注意:感謝您的答案,但輸出錯誤。我對此很抱歉。事實上,它似乎可能有一個簡單的解決方案,但我不是很有經驗。謝謝。 – user295744