有没有其他/更好的方法来做到这一点?它必须是这个结构,我不能改变。
<xml>
<animal house="1">
<home>Cat</home>
<home>Dog</home>
<outside>Dove</outside>
<outside>Parrot</outside>
</animal>
<animal house="2">
<home>Turtle</home>
<home>Snake</home>
<outside>Bee</outside>
<outside>Horse</outside>
</animal>
</xml>现在我需要从所有的房子中获取家养的动物,并加入价值
这是可行的,但是我想知道是否有使用xpath的其他方法
http://xsltransform.net/3MEbY7g
<xsl:for-each select="./animal">
<xsl:variable name="temp" >
<xsl:copy-of select="./home"/>
</xsl:variable>
<xsl:value-of select="$temp"/>
</xsl:for-each>发布于 2018-07-11 04:45:59
只能使用XPath-2.0或更高版本。所以试试这个XPath-2.0表达式:
string-join(for $a in /xml/animal return $a/home/text(),' - ')它的输出是
Cat - Dog - Turtle - SnakeXPath表达式的最后一部分是分隔符。
在XPath-1.0中,您无法做到这一点。您可以选择的唯一集合是
/xml/animal/home它选择了所有的“家里的动物”。
发布于 2018-07-11 05:31:20
你的代码
<xsl:for-each select="./animal">
<xsl:variable name="temp" >
<xsl:copy-of select="./home"/>
</xsl:variable>
<xsl:value-of select="$temp"/>
</xsl:for-each>(a)非常冗长,并且(b)在XSLT 1.0下不会产生所需的输出。
在XSLT1.0中,可以使用以下命令获得所需的输出
<xsl:for-each select="animal/home">
<xsl:value-of select="."/>
</xsl:for-each>在XSLT 2.0中,您可以这样写:
<xsl:value-of select="animal/home" separator=""/> 使用单个XPath 2.0表达式也可以获得相同的结果:
string-join(/*/animal/home, "")但是没有XPath 1.0的等价物。
https://stackoverflow.com/questions/51273537
复制相似问题