2011-12-20 65 views
1

目前我收到這個如何顯示的第一個孩子只有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。 請幫忙

回答

2

這個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> 
+0

謝謝你幫我 – almightyBob 2011-12-20 14:20:14

0

,這是好事,知道如何將其覆蓋,以實現最大的靈活性

.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> 
相關問題