我在我的项目中使用XSLT 1.0。在我的XSLT转换中,我必须检查一个特定的元素,如果该元素存在,我必须执行一些连接或其他连接操作。
然而,我在这里找不到一个选项,比如一些内置函数。
需求是这样的
<Root>
<a></a>
<b></b>
<c></c>
</Root>这里是元素<a>,传入请求有效载荷,然后我们需要执行<b>和<c>的连接,否则<c>和<b>。
发布于 2015-12-18 00:50:11
您可以使用模板匹配来完成此操作:
<xsl:template match="Root[not(a)]">
<xsl:value-of select="concat(c, b)"/>
</xsl:template>
<xsl:template match="Root[a]">
<xsl:value-of select="concat(b, c)"/>
</xsl:template>发布于 2015-12-18 00:50:29
试着这样做:
<xsl:template match="/Root">
<xsl:choose>
<xsl:when test="a">
<!-- do something -->
</xsl:when>
<xsl:otherwise>
<!-- do something else -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>解释:该测试返回由表达式a选择的节点集的布尔值。如果node-set不为空,则结果为true。
发布于 2015-12-18 00:50:44
使用xsl:choose测试元素是否存在
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/Root">
<xsl:choose>
<xsl:when test="a">
<xsl:value-of select="concat(c, b)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(b, c)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>或在用于模板匹配的谓词中:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/Root[a]">
<xsl:value-of select="concat(c, b)"/>
</xsl:template>
<xsl:template match="/Root[not(a)]">
<xsl:value-of select="concat(b, c)"/>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/34338654
复制相似问题