【发布时间】:2015-01-20 20:39:08
【问题描述】:
在 XSL 中是否可以检查字符串是否存在于字符串数组中? 我有以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<ItemsArr>
<it attr="DANCHO">
<title>1</title>
</it>
<it attr="DAN">
<title>2</title>
</it>
<it attr="IVANCHO">
<title>3</title>
</it>
<it attr="DRAGANCHO">
<title>4</title>
</it>
<it attr="PETKANCHO">
<title>5</title>
</it>
<keys>
<itemKey>DANCHO</itemKey>
<itemKey>THISISONLYFORTESTING</itemKey>
</keys>
</ItemsArr>
以及以下 XSL 转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:variable name="items" select="ItemsArr/keys/itemKey"/>
<xsl:for-each select="/ItemsArr/it[contains($items,@attr)]">
<tr>
<td><xsl:value-of select="@attr"/></td>
<td><xsl:value-of select="title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我想将所有键 (ItemsArr/Keys) 存储在一个数组 ($items) 中,然后遍历 it-s,该 attr 值存在于 $items 数组中。 请注意,对于上述数据,两者都被选中。 如果交换了两个键(DANCHO 和 THISISONLYFORTESTING),则不会选择任何内容。
【问题讨论】: