Html 如何使用 XSLT 选择第一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1498819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I select the first element using XSLT?
提问by John Bubriski
I have a list of news items, sorted by dateCreated. I have a preview box control where I only want to show the first item. How can I do that using XSLT?
我有一个按日期创建的新闻项目列表。我有一个预览框控件,我只想显示第一项。我怎样才能使用 XSLT 做到这一点?
<xml>
<news>
<newsitem>
<dateCreated>2009-09-09</dateCreated>
<summary>Something great happened</sumamry>
</newsitem>
<newsitem>
<dateCreated>2009-09-08</dateCreated>
<summary>Something bad happened</sumamry>
</newsitem>
<newsitem>
<dateCreated>2009-09-07</dateCreated>
<summary>Something really bad happened</sumamry>
</newsitem>
</news>
</xml>
回答by brianary
If you wish to output XHTML 1.1, here's one way:
如果你想输出 XHTML 1.1,这里有一种方法:
<?xml version="1.0"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsl xs">
<xsl:output mode="xhtml" version="1.1" omit-xml-declaration="yes"
encoding="utf-8" media-type="application/xhtml+xml" indent="no"
doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
<xsl:template match="//newsItem[1]">
<div><xsl:value-of select="dateCreated"/></div>
<div><xsl:value-of select="summary"/></div>
</xsl:template>
</xsl:transform>
回答by knittl
//newsItem[1]
should do
应该做
回答by mmoossen
I had the same question and I think I found a better answer:
我有同样的问题,我想我找到了更好的答案:
<xsl:for-each select="newsItem[1]">
<div><xsl:value-of select="dateCreated"/></div>
<div><xsl:value-of select="summary"/></div>
</xsl:for-each>
回答by user2113770
//newsItem[1]
Selects the first book newsItem
element, but note that IE5 and later has implemented that [0]
should be the first node, but according to the W3C standard it should be [1]
!
选择第一个 booknewsItem
元素,但请注意 IE5 及更高版本已经实现,[0]
应该是第一个节点,但根据 W3C 标准,它应该是[1]
!