我正在从XML转换为HTML,我只希望显示包含特定元素的段落。我该怎么做?
我的XML看起来像这样:
<div id="container">
<ul>
<li>
<div class="resource-name">Room A1</div>
<div class="time-row">
<div class="time-col">12a</div>
<div class="time-col">1a</div>
<div class="time-col">2a</div>
<div class="time-col">3a</div>
<div class="time-col">4a</div>
<div class="time-col">5a</div>
<div class="time-col">6a</div>
<div class="time-col">7a</div>
<div class="time-col">8a</div>
<div class="time-col">9a</div>
<div class="time-col">10a</div>
<div class="time-col">11a</div>
<div class="time-col">12p</div>
<div class="time-col">1p</div>
<div class="time-col">2p</div>
<div class="time-col">3p</div>
<div class="time-col">4p</div>
<div class="time-col">5p</div>
<div class="time-col">6p</div>
<div class="time-col">7p</div>
<div class="time-col">8p</div>
<div class="time-col">9p</div>
<div class="time-col">10p</div>
<div class="time-col">11p</div>
</div>
</li>
</ul>
</div>
所以我想输出只包含第一个和第四个段落的HTML。
到目前为止,我已经有了这个。
<?xml version="1.0" encoding="UTF-8"?>
<text>
<p>This paragraph contains the <bingo>information</bingo> I want</p>
<p>This paragraph doesn't.</p>
<p>This paragraph doesn't</p>
<p>This paragraph contains the <nest><bingo>information</bingo></nest> I want, too</p>
<p>This paragraph doesn't</p>
</text>
这显然是非常错误的。但我不知道我应该如何思考它。我会非常感谢任何帮助。
答案 0 :(得分:1)
如果您只想选择具有p
后代的bingo
个元素,那么您想要的表达式为:
<xsl:for-each select="text/p[descendant::bingo]">
这也可以写成......
<xsl:for-each select="text/p[.//bingo]">
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h1>Bingo</h1>
<div id="results">
<xsl:for-each select="text/p[.//bingo]">
<p>
<xsl:value-of select="."/>
</p>
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
使用模板规则,而不是循环和条件:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h1>Bingo</h1>
<div id="results">
<xsl:apply-templates>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="p[.//bingo]">
<p><xsl:value-of select="."/></p>
</xsl:template>
<xsl:template match="p"/>
</xsl:stylesheet>
答案 2 :(得分:0)
所以我想输出只包含第一个段落的HTML 第四次
只需使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<html>
<body>
<h1>Bingo</h1>
<div id="results">
<xsl:copy-of select="p[.//bingo]"/>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>