【发布时间】:2017-01-20 01:36:54
【问题描述】:
我有一个文件doc.xml:
<?xml version="1.0"?>
<!-- This is the first top-level node -->
<!-- This is the second top-level node -->
<ThirdNode/>
<!-- This is the fourth top-level node -->
我想从文档中选择第一个顶级节点,在firstnode.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/node()[1]">First</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
当我运行xsltproc firstnode.xsl doc.xml 时,我得到:
<?xml version="1.0"?>
FirstFirst<ThirdNode/><!-- This is the fourth top-level node -->
… 而不是预期的输出:
<?xml version="1.0"?>
First
<!-- This is the second top-level node -->
<ThirdNode/>
<!-- This is the fourth top-level node -->
为什么/node()[1] 选择器不仅仅匹配第一条评论?如何选择文档的第一个节点(无论是注释、元素还是其他)?
【问题讨论】:
-
试试
(/node())[1] -
@eLRuLL 我试过了,得到了
xsltCompilePattern : failed to compile '(/node())[1]' -
备案:xsltproc 是针对 libxml 20902、libxslt 10128 和 libexslt 817 编译的; libxslt 10128 是针对 libxml 20902 编译的; libexslt 817 是针对 libxml 20902 编译的