2012-02-15 68 views
2

我有以下XML結構(這是很長,在現實例子compicated)的xsl:比賽和xsl:同一母公司中應用

<document> 
    <node1> 
     <child1/> 
     <child2/> 
     ... 
    </node1> 
    <anotherNode /> 
</document> 

我想創建像

<xsl:template match="node1" mode="node1"> 
     <img alt="" src="{child1}" /> 
     ... 
     ... 

</xsl:template> 
模板

而在另一個模板套用這個範本,這樣

<xsl:template match="anotherNode"> 
    <xsl:apply-templates select="node1" mode="node1" /> 
</xsl:template> 

如果節點1標籤有父母會很容易d o,但我無法弄清楚如何匹配和應用模板,如果它沒有父。

+0

在你的榜樣,節點1確實有父母 - 的'document'元素。所有元素都有父母。 – 2012-02-15 16:56:14

+0

我知道,但如果我選擇文檔標籤模板輸出的所有內容,而不是我需要的那個 – tylik 2012-02-15 17:01:29

回答

2

使用

<xsl:template match="anotherNode"> 
    <xsl:apply-templates select="/*/node1" mode="node1" /> 
</xsl:template> 

或可替代

<xsl:template match="anotherNode"> 
    <xsl:apply-templates select="../node1" mode="node1" /> 
</xsl:template> 
+0

啊,這是我的錯我的XML的結構更加複雜,現在我看到它的工作 – tylik 2012-02-15 17:20:31

+0

@tylik:不客氣。 – 2012-02-15 17:26:31

1

嘛貼輸入不連格式良好(根元素的開始標記中的拼寫「docuemnt」?)和indentatio N也還不清楚,但只要anotherNode元素和node1元素是同級的,你可以做

<xsl:template match="anotherNode"> 
    <xsl:apply-templates select="preceding-sibling::node1" mode="node1"/> 
</xsl:template>