问题描述
我需要在xslt模板中声明一个数组,例如say(伪代码)
myarray = [] //声明空数组
然后我需要一次将一些字符串放在下面
myarray = [doc] // put a string into the array
然后我需要检索数组中的每个元素,并与另一个字符串进行比较,例如
foreach myarray[i]{
tempstring = myarray[i]
test=compare(somes-tring,tempstring)
then
do some action
break
else
continue loop till all element in array are compared
` }`
然后,如果数组中没有字符串与“ some-string”匹配,则将some-string附加到数组中。
经过一些迭代之后,空myarray结束了
解决方法
您可以这样声明一个动态数组:
<xsl:variable name="myarray">
<item ord="-1" index="-1"/>
</xsl:variable>
带有一些测试的代码(我假设您已经学习了如何在XSLT中编写循环,因此在这里我不做更多详细介绍):
代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="myarray">
<item ord="-1" index="-1"/>
</xsl:variable>
<xsl:template match="/">
<xsl:param name="index"/>
<xsl:param name="value"/>
<xsl:if test="./*[@index = $index] and $value">
<xsl:variable name="myOrd" select="./*[@index = $index]/@ord"/>
<xsl:copy-of select="./*[@ord < $myOrd]"/>
<item ord="{./*[@index = $index]/@ord}" index="{$index}"><xsl:value-of select="$value"/></item>
<xsl:copy-of select="./*[@ord > $myOrd]"/>
</xsl:if>
<xsl:if test="./*[@index = $index] and not($value)">
<xsl:variable name="myOrd" select="./*[@index = $index]/@ord"/>
<xsl:copy-of select="./*[@ord < $myOrd]"/>
<xsl:copy-of select="./*[@ord > $myOrd]"/>
</xsl:if>
<xsl:if test="not(./*[@index = $index])">
<xsl:variable name="maxOrd" select="number(./*[last()]/@ord + 1)"/>
<xsl:copy-of select="./*[@ord < $maxOrd]"/>
<item ord="{$maxOrd}" index="{$index}"><xsl:value-of select="$value"/></item>
</xsl:if>
</xsl:template>
<xsl:template name="test">
<!-- start of the loop -->
<!-- iteration step 1 -->
<xsl:variable name="myarray">
<xsl:apply-templates select="$myarray">
<xsl:with-param name="index" select="3"/>
<xsl:with-param name="value" select="'value at 3'"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$myarray" mode="dump"/>
<dump step="1">
<xsl:copy-of select="$myarray/*[@ord >= 0]"/>
</dump>
<!-- iteration step 2 -->
<xsl:variable name="myarray">
<xsl:apply-templates select="$myarray">
<xsl:with-param name="index" select="15"/>
<xsl:with-param name="value" select="'value at 15'"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$myarray" mode="dump"/>
<dump step="2">
<xsl:copy-of select="$myarray/*[@ord >= 0]"/>
</dump>
<!-- iteration step 3 -->
<xsl:variable name="myarray">
<xsl:apply-templates select="$myarray">
<xsl:with-param name="index" select="5"/>
<xsl:with-param name="value" select="'value at 5'"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$myarray" mode="dump"/>
<dump step="3">
<xsl:copy-of select="$myarray/*[@ord >= 0]"/>
</dump>
<!-- iteration step 4 -->
<xsl:variable name="myarray">
<xsl:apply-templates select="$myarray">
<xsl:with-param name="index" select="5"/>
<xsl:with-param name="value" select="'newValue at 5'"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$myarray" mode="dump"/>
<dump step="4">
<xsl:copy-of select="$myarray/*[@ord >= 0]"/>
</dump>
<!-- iteration step 5 -->
<xsl:variable name="myarray">
<xsl:apply-templates select="$myarray">
<xsl:with-param name="index" select="15"/>
<xsl:with-param name="value" select="''"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:apply-templates select="$myarray" mode="dump"/>
<dump step="5">
<xsl:copy-of select="$myarray/*[@ord >= 0]"/>
</dump>
<!-- end of the loop -->
</xsl:template>
<xsl:template match="/" mode="dump">
<xsl:message>Array:</xsl:message>
<xsl:for-each select="*[@index > -1]">
<xsl:sort select="@index" data-type="number"/>
<xsl:message>
<xsl:value-of select="concat('array[',@index,'] = ',.)"/>
</xsl:message>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
评论
- 第1步:在索引3处设置值
- 第2步:在索引15处设置值
- 第3步:在索引5处设置值
- 第4步:将值设置为索引5(新值)
- 第5步:删除索引15处的值
控制台输出
Array:
array[3] = value at 3
Array:
array[3] = value at 3
array[15] = value at 15
Array:
array[3] = value at 3
array[5] = value at 5
array[15] = value at 15
Array:
array[3] = value at 3
array[5] = newValue at 5
array[15] = value at 15
Array:
array[3] = value at 3
array[5] = newValue at 5
XML输出
<?xml version="1.0" encoding="UTF-8"?>
<dump step="1">
<item ord="0" index="3">value at 3</item>
</dump>
<dump step="2">
<item ord="0" index="3">value at 3</item>
<item ord="1" index="15">value at 15</item>
</dump>
<dump step="3">
<item ord="0" index="3">value at 3</item>
<item ord="1" index="15">value at 15</item>
<item ord="2" index="5">value at 5</item>
</dump>
<dump step="4">
<item ord="0" index="3">value at 3</item>
<item ord="1" index="15">value at 15</item>
<item ord="2" index="5">newValue at 5</item>
</dump>
<dump step="5">
<item ord="0" index="3">value at 3</item>
<item ord="2" index="5">newValue at 5</item>
</dump>