测试XML节点是否为空,如果是,则硬编码href属性值|如果不是空,则使用XMSL使用href属性的值



我有从用户输入中获取值的XML。我想测试一下url节点是否为空。

XSLT处理器是Saxon 11.3

如果为空

  • 然后我想在href中插入一个值

如果不是空白

  • 我想在href中使用节点的值

我有以下代码,但它不起作用。我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="unique-component-name/card">
<html>
<body>

<xsl:choose>
<xsl:when test="(url = '')">
<a href="https://example.com">
</xsl:when>
<xsl:otherwise>
<a href="{url}">
</xsl:otherwise>
</xsl:choose>

<h3><xsl:value-of select="level" /></h3>
<h4><xsl:value-of select="name" /></h4>
<h5><xsl:value-of select="location" /></h5>
</a>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

这是我的XML的一个例子

<?xml version="1.0" encoding="UTF-8"?>

<unique-component-name>

<card>

<url></url>
<level>Level Value</level>
<name>Name Value</name>
<location>Location Value</location>

</card>

</unique-component-name>

HTML中的预期结果

<a href="https://example.com">
<h3>Level Value</h3>
<h4>Name Value</h4>
<h5>Location Value</h5>
</a>

我得到了什么

<!-- Result with blank url node -->
<a href="https://example.com">
<!-- h3, h4, h5 missing -->
</a>
<!-- Result when url node is populated -->
<!-- Nothing, No HTML elements -->

这将是一种构建a元素并有条件地设置href属性的方法

<xsl:element name="a">
<xsl:choose>
<xsl:when test="(url = '')">
<xsl:attribute name="href">https://example.com</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<h3><xsl:value-of select="level"/></h3>
<h4><xsl:value-of select="name"/></h4>
<h5><xsl:value-of select="location"/></h5>
</xsl:element>

您没有指定使用的XSLT版本。在XSLT2及更高版本中,可以执行以下操作:

<a href="{(url[normalize-string()], 'https://example.com')[1]}">
<h3><xsl:value-of select="level"/></h3>
<h4><xsl:value-of select="name"/></h4>
<h5><xsl:value-of select="location"/></h5>
</a>

NB表达式(url[normalize-string()], 'https://example.com')[1]的意思是:

序列中的第一项包括:

  • url元素(如果不是空白(,以及
  • 字符串'https://example.com'

如果url元素丢失或为空,则序列将只包含一个项;字符串CCD_ 7;因此,这将是序列中的第一个项目,并将由谓词[1]返回

这个习语(a, b, c)[1]经常用于XPath2及更高版本;它允许您选择某个数据项a,但如果它丢失,则回退到某个其他数据段b,如果第二个备选项也丢失,则再次回退(到c(。。。

我有以下代码,但它不起作用。我做错了什么?

"不工作";不是一个问题的好描述。你需要告诉我们它是如何失败的。在这一点上,我们只能说您的XSLT代码不是格式良好的XML。您尚未关闭a元素。代替:

<xsl:choose>
<xsl:when test="(url = '')">
<a href="https://example.com">
</xsl:when>
<xsl:otherwise>
<a href="{url}">
</xsl:otherwise>
</xsl:choose>

它需要:

<xsl:choose>
<xsl:when test="(url = '')">
<a href="https://example.com"/>
</xsl:when>
<xsl:otherwise>
<a href="{url}"/>
</xsl:otherwise>
</xsl:choose>

或者,如果您确实需要将标题插入锚:

<a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="(url = '')">https://example.com</xsl:when>
<xsl:otherwise>
<xsl:value-of select="url"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<h3>
<xsl:value-of select="level"/>
</h3>
<h4>
<xsl:value-of select="name"/>
</h4>
<h5>
<xsl:value-of select="location"/>
</h5>
</a>

(请注意,这是与XSLT1.0兼容的语法(

当然,您必须去掉末尾的孤立</a>结束标记。

最新更新