这是我的XSLT 1.0代码:
<xsl:for-each select = "segment">
<xsl:if test ="position() != 1 or position() != last()">
<notfirstorlast></notfirstorlast>
</xsl:if>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
这应该添加一个<notfirstorlast>
元素,在所有<segment>
节点中为第一个和最后一个节点exepct.但它不起作用.它将在没有或声明的情况下工作.这个作品:
<xsl:if test ="position() != 1>
Run Code Online (Sandbox Code Playgroud)
我或声明有问题.
必须满足这两个条件,因此您必须使用"和"而不是"或":
<xsl:if test ="position() != 1 and position() != last()">
Run Code Online (Sandbox Code Playgroud)
我或声明有问题.
对,就是这样.使用"或",所有元素都有资格获得notfirstorlast
元素,因为所有元素都是"不是第一个"或"不是最后一个"元素.
输入
<?xml version="1.0" encoding="UTF-8"?>
<root>
<segment/>
<segment/>
<segment/>
</root>
Run Code Online (Sandbox Code Playgroud)
样式表
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:for-each select = "segment">
<xsl:copy>
<xsl:if test ="position() != 1 and position() != last()">
<notfirstorlast></notfirstorlast>
</xsl:if>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
产量
<?xml version="1.0" encoding="utf-8"?>
<segment/>
<segment>
<notfirstorlast/>
</segment>
<segment/>
Run Code Online (Sandbox Code Playgroud)