我有从用户输入中获取值的XML。我想测试url节点是否为空。
XSLT处理器是Saxon 11.3
如果它是空白的
中。
如果不是空白,则为
中使用节点的值
我有下面的代码,但它不起作用。我做错了什么?
<?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>
在中的预期结果
<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 -->
发布于 2022-09-27 22:17:24
这将是构建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>
https://stackoverflow.com/questions/73874130
复制相似问题