基本上我需要一個接一個地重復每個子回圈兩次。在下面的示例中,“apple”應該重復兩次,然后“mango”應該重復兩次
XML:
<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
<root >
<child id="123">
<fruit>apple</fruit>
<comment>This is 1st line</comment>
</child>
<child id="345">
<fruit>mango</fruit>
<comment>This is 2nd line</comment>
</child>
</root>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:output indent="yes" />
<xsl:template match="/">
<xsl:param name="pack" select="2"></xsl:param>
<xsl:for-each select="root/child">
<xsl:for-each select="(//node())[position() <= $pack]">
<xsl:text>
</xsl:text>
<xsl:value-of select="//fruit"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="//comment"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
當前輸出:
蘋果這是第一行
蘋果這是第一行
蘋果這是第一行
蘋果這是第一行
預期的:
蘋果這是第一行
蘋果這是第一行
芒果 這是第二行
芒果 這是第二行
非常感謝您的幫助!
uj5u.com熱心網友回復:
在XSLT 1.0中,您需要執行以下操作:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pack" select="2"/>
<xsl:template match="child" name="repeat">
<xsl:param name="times" select="$pack"/>
<xsl:if test="$times > 0">
<xsl:value-of select="fruit"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="comment"/>
<xsl:text>
</xsl:text>
<!-- recursive call -->
<xsl:call-template name="repeat">
<xsl:with-param name="times" select="$times - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/489176.html
下一篇:比較多個串列中的數字Python
