如何显示的第一个孩子只有XSL
问题描述:
目前我收到这个如何显示的第一个孩子只有XSL
<root>
<event>bla</event>
</root>
我想不仅是
<event>bla</event>
我的XSL是这样
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:param name="Number" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/root/event" />
<xsl:template match="/root/event[1]">
<xsl:copy-of select="current()" />
</xsl:template>
</xsl:stylesheet>
我无法首先查看如何访问第一个节点,而无需超过/ root。 请帮忙
答
这个XSLT应该回答你的问题。它将给event
元素是他们的父节点的第一个孩子:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="event[1]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
的root
元素由match="*"
模板跳过。
另一种方式来做到这一点(更简单但不太进化):由于您使用的是身份规则
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:copy-of select="root/event[1]"/>
</xsl:template>
</xsl:stylesheet>
答
,这是好事,知道如何将其覆盖,以实现最大的灵活性:
.1。替代元素,但仍处理其子树中的所有节点的覆盖:
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
.2。覆写排除它,并从它的子树中的任何节点的元素:当在应用这种转变
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="event[position() > 1]"/>
</xsl:stylesheet>
:
<xsl:template match="event[position() > 1]"/>
这两个结合给我们完整的通缉改造下面的XML文档:
<root>
<event>bla1</event>
<event>bla2</event>
</root>
ŧ他想要正确的结果产生:
<event>bla1</event>
谢谢你帮我 – almightyBob 2011-12-20 14:20:14