我需要在我的 xslt 中写一个子句,说明如果元素存在则显示文本节点,如果不存在则不显示任何内容。如果文本节点是一个特定的词,但如果元素存在,我可以找到如何编写。
非常感谢任何建议。
PS:xslt/xml 等新手
例如:XML 代表一本包含页面的书。该页面的一个版本有一个标题。下面是一个有四列和 20 行的表。在这下面是一个页脚。此页脚不在页面的其他版本上。我的 xslt 需要将 xml 转换为可视化表示此内容的网页。
因此 xml 有一个元素 <Footer>
它在架构中的 minOccurs 为 0。
最佳答案
这可以通过省略比较来完成,例如
<xsl:if test='root/element'>
但是,最终获得您想要的东西的一种更简洁的方法是使用 xsl:templates
所以对于 xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<page>
<title>Franks</title>
<header>header text</header>
<bodytext>here is the body text</bodytext>
</page>
<page>
<title>Joes</title>
<footer>footer text</footer>
<bodytext>here is the body text2</bodytext>
</page>
</root>
xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="page"></xsl:apply-templates>
</xsl:template>
<xsl:template match="page">
<h1>
<xsl:value-of select="title"/>
</h1>
<p>
<xsl:value-of select="bodytext"/>
</p>
<xsl:apply-templates select="footer"/>
</xsl:template>
<xsl:template match="footer">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
说明了如何做到这一点。 查看w3schools xpath tutorial有关选择的更多信息。
关于xml - xsl :if - if element is present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8932365/