Xml 使用模板匹配测试节点是否为空或包含NULL
编辑到XML 我试图弄清楚如何使用模板匹配测试节点是否不存在或是否包含单词NULL。目前我正在匹配的模板中使用测试。有没有更有效的方法来做这个测试 在XML中,如果或节点不包含eixt,或包含NULL一词,则我希望完全跳过该节点,并打印一条语句,说明该节点不正确的原因(即“标题不存在”或“内容为NULL”) 在XML中,第三个和第四个节点不应该显示任何内容,而是打印一条语句,说明为什么不显示内容 下面是我用来测试的XML示例:Xml 使用模板匹配测试节点是否为空或包含NULL,xml,xslt,Xml,Xslt,编辑到XML 我试图弄清楚如何使用模板匹配测试节点是否不存在或是否包含单词NULL。目前我正在匹配的模板中使用测试。有没有更有效的方法来做这个测试 在XML中,如果或节点不包含eixt,或包含NULL一词,则我希望完全跳过该节点,并打印一条语句,说明该节点不正确的原因(即“标题不存在”或“内容为NULL”) 在XML中,第三个和第四个节点不应该显示任何内容,而是打印一条语句,说明为什么不显示内容 下面是我用来测试的XML示例: <?xml version="1.0" encoding="u
<?xml version="1.0" encoding="utf-8"?>
<FIGURES>
<FIGURE>
<TITLE>Title 1</TITLE>
<DESC>Description 1</DESC>
<CONTENT>Content 1</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 2</TITLE>
<DESC>Description 2</DESC>
<CONTENT>Content 2</CONTENT>
</FIGURE>
<FIGURE>
<DESC>Description 3</DESC>
<CONTENT>Content 3</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 4</TITLE>
<DESC>Description 4</DESC>
<CONTENT>NULL</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 5</TITLE>
<DESC>Description 5</DESC>
<CONTENT>Content 5</CONTENT>
</FIGURE>
</FIGURES>
标题1
说明1
内容1
标题2
说明2
内容2
说明3
内容3
标题4
说明4
无效的
标题5
说明5
内容5
以下是我正在使用的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<div class="container">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="FIGURES">
<ul class="list">
<xsl:apply-templates select="FIGURE" mode="titles" />
</ul>
<div class="content">
<xsl:apply-templates select="FIGURE" mode="content" />
</div>
</xsl:template>
<xsl:template match="FIGURE" mode="titles">
<xsl:choose>
<!-- Check to see if the TITLE field is empty -->
<xsl:when test="translate(./TITLE,' ', '') = ''">The TITLE node is empty</xsl:when>
<!-- Check to see if the TITLE field is NULL -->
<xsl:when test="normalize-space(./TITLE) = 'NULL'">The TITLE node is NULL</xsl:when>
<!-- Check to see if the CONTENT field is empty -->
<xsl:when test="translate(./CONTENT,' ', '') = ''">The CONTENT node is empty</xsl:when>
<!-- Check to see if the CONTENT field is NULL -->
<xsl:when test="normalize-space(./CONTENT) = 'NULL'">The CONTENT node is NULL</xsl:when>
<xsl:otherwise>
<li>
<a href="#section-{position()}">
<h3><xsl:value-of select="TITLE" /></h3>
</a>
<p><xsl:value-of select="DESC" /></p>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="FIGURE" mode="content">
<xsl:choose>
<!-- Check to see if the TITLE field is empty -->
<xsl:when test="translate(./TITLE,' ', '') = ''">The TITLE node is empty</xsl:when>
<!-- Check to see if the TITLE field is NULL -->
<xsl:when test="normalize-space(./TITLE) = 'NULL'">The TITLE node is NULL</xsl:when>
<!-- Check to see if the CONTENT field is empty -->
<xsl:when test="translate(./CONTENT,' ', '') = ''">The CONTENT node is empty</xsl:when>
<!-- Check to see if the CONTENT field is NULL -->
<xsl:when test="normalize-space(./CONTENT) = 'NULL'">The CONTENT node is NULL</xsl:when>
<xsl:otherwise>
<div id="section-{position()}">
<p><xsl:value-of select="CONTENT" /></p>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
标题节点为空
标题节点为空
内容节点为空
内容节点为空
标题节点为空
标题节点为空
内容节点为空
内容节点为空
谢谢。这将产生与XSLT相同的结果,并避免使用
xsl:choose
。请试一试:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<div class="container">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="FIGURES">
<ul class="list">
<xsl:apply-templates select="FIGURE" mode="titles" />
</ul>
<div class="content">
<xsl:apply-templates select="FIGURE" mode="content" />
</div>
</xsl:template>
<xsl:template match="FIGURE" mode="titles">
<li>
<a href="#section-{position()}">
<h3>
<xsl:value-of select="TITLE" />
</h3>
</a>
<p>
<xsl:value-of select="DESC" />
</p>
</li>
</xsl:template>
<xsl:template
match="FIGURE[ *[self::TITLE or self::CONTENT]
[not(normalize-space(.)) or normalize-space(.) = 'NULL']
]"
mode="titles">
<xsl:apply-templates select="TITLE | CONTENT" />
</xsl:template>
<xsl:template match="FIGURE" mode="content">
<div id="section-{position()}">
<p>
<xsl:value-of select="CONTENT" />
</p>
</div>
</xsl:template>
<xsl:template
match="FIGURE[ *[self::TITLE or self::CONTENT]
[not(normalize-space(.)) or normalize-space(.) = 'NULL']
]"
mode="content">
<xsl:apply-templates select="TITLE | CONTENT" />
</xsl:template>
<xsl:template match="TITLE | CONTENT" />
<xsl:template match="*[self::TITLE or self::CONTENT][not(normalize-space(.))]">
<xsl:value-of select="concat('The ', local-name(), ' node is empty')"/>
</xsl:template>
<xsl:template match="*[self::TITLE or self::CONTENT][normalize-space(.) = 'NULL']">
<xsl:value-of select="concat('The ', local-name(), ' node is NULL')"/>
</xsl:template>
</xsl:stylesheet>
Please give it a try.
请试一试。
这里是一个较短的转换,它产生相同的结果,并且不使用任何条件XSLT指令:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<div class="container">
<ul class="list">
<xsl:apply-templates/>
</ul>
<div class="content">
<xsl:apply-templates mode="content"/>
</div>
</div>
</xsl:template>
<xsl:template match="FIGURE">
<li>
<a href="#section-{position()}">
<xsl:apply-templates select="TITLE"/>
</a>
<xsl:apply-templates select="DESC"/>
</li>
</xsl:template>
<xsl:template match="TITLE">
<h3><xsl:value-of select="."/></h3>
</xsl:template>
<xsl:template match="DESC">
<p><xsl:value-of select="."/></p>
</xsl:template>
<xsl:template match="FIGURE[*[. = 'NULL' or not(normalize-space())]]">
<xsl:apply-templates select="*[. = 'NULL' or not(normalize-space())]"/>
</xsl:template>
<xsl:template match="FIGURE/*[. = 'NULL']">
The <xsl:value-of select="name()"/> node is NULL.
</xsl:template>
<xsl:template match="FIGURE/*[not(normalize-space())]">
The <xsl:value-of select="name()"/> node is empty.
</xsl:template>
<xsl:template match="FIGURE[not(*[. = 'NULL' or not(normalize-space())])]" mode="content">
<div id="section-{position()}">
<p><xsl:value-of select="CONTENT"/></p>
</div>
</xsl:template>
<xsl:template match="FIGURE" mode="content"/>
</xsl:stylesheet>
节点为空。
节点为空。
在提供的XML文档上应用此转换时:
<FIGURES>
<FIGURE>
<TITLE>Title 1</TITLE>
<DESC>Description 1</DESC>
<CONTENT>Content 1</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 2</TITLE>
<DESC>Description 2</DESC>
<CONTENT>Content 2</CONTENT>
</FIGURE>
<FIGURE>
<TITLE></TITLE>
<DESC>Description 3</DESC>
<CONTENT>Content 3</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 4</TITLE>
<DESC>Description 4</DESC>
<CONTENT>NULL</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 5</TITLE>
<DESC>Description 5</DESC>
<CONTENT>Content 5</CONTENT>
</FIGURE>
</FIGURES>
<div class="container">
<ul class="list">
<li>
<a href="#section-1">
<h3>Title 1</h3>
</a>
<p>Description 1</p>
</li>
<li>
<a href="#section-2">
<h3>Title 2</h3>
</a>
<p>Description 2</p>
</li>
The TITLE node is empty.
The CONTENT node is NULL.
<li>
<a href="#section-5">
<h3>Title 5</h3>
</a>
<p>Description 5</p>
</li>
</ul>
<div class="content">
<div id="section-1">
<p>Content 1</p>
</div>
<div id="section-2">
<p>Content 2</p>
</div>
<div id="section-5">
<p>Content 5</p>
</div>
</div>
</div>
标题1
说明1
内容1
标题2
说明2
内容2
说明3
内容3
标题4
说明4
无效的
标题5
说明5
内容5
产生与最初提供的转换相同的结果:
<FIGURES>
<FIGURE>
<TITLE>Title 1</TITLE>
<DESC>Description 1</DESC>
<CONTENT>Content 1</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 2</TITLE>
<DESC>Description 2</DESC>
<CONTENT>Content 2</CONTENT>
</FIGURE>
<FIGURE>
<TITLE></TITLE>
<DESC>Description 3</DESC>
<CONTENT>Content 3</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 4</TITLE>
<DESC>Description 4</DESC>
<CONTENT>NULL</CONTENT>
</FIGURE>
<FIGURE>
<TITLE>Title 5</TITLE>
<DESC>Description 5</DESC>
<CONTENT>Content 5</CONTENT>
</FIGURE>
</FIGURES>
<div class="container">
<ul class="list">
<li>
<a href="#section-1">
<h3>Title 1</h3>
</a>
<p>Description 1</p>
</li>
<li>
<a href="#section-2">
<h3>Title 2</h3>
</a>
<p>Description 2</p>
</li>
The TITLE node is empty.
The CONTENT node is NULL.
<li>
<a href="#section-5">
<h3>Title 5</h3>
</a>
<p>Description 5</p>
</li>
</ul>
<div class="content">
<div id="section-1">
<p>Content 1</p>
</div>
<div id="section-2">
<p>Content 2</p>
</div>
<div id="section-5">
<p>Content 5</p>
</div>
</div>
</div>
-
说明1
-
说明2
标题节点为空。
内容节点为空。
-
说明5
内容1
内容2
内容5
很抱歉,我在最初发布的XML中犯了一个错误。我编辑了问题和XML以反映这一点。我一直试图预测不包含值的节点,但实际上,如果没有值,该节点将不存在。我想这就是为什么我在这个解决方案上遇到麻烦的原因,这个解决方案本来是有效的。我也向你道歉。此解决方案适用于我原来的问题,但我在发布此问题时犯了一个错误。当XML节点不包含任何值时,它不会显示。例如,如果没有值,它将不会显示为。该节点在XML中根本不存在。在or节点不存在的情况下,我一直致力于让此解决方案正常工作。很抱歉之前在我的XML示例中发布了不正确的内容。我编辑了它以反映形势。