2009-11-18 58 views
7

複製的代碼是什麼,我可以用在<xsl:copy-of select="tag"/>替換,當應用於以下XML ..的xsl:不包括父

<tag> 
    content 
    <a> 
    b 
    </a> 
</tag> 

..would給出的結果:

content 
<a> 
    b 
</a> 

我想其中的呼應了所有的內容,但不包括父標籤


基本上我有內容的幾個部分在我的XML文件,HTML格式,在XML標記分組
我希望有條件地訪問它們&呼應出來
例如:<xsl:copy-of select="description"/>
額外父生成標籤不影響瀏覽器渲染,但他們是無效的標籤,&我寧願能夠將它們刪除
我是否完全錯誤地解決這個問題?

回答

12

既然你要包括content部分,以及,你需要的node()功能,而不是*操作:

<xsl:copy-of select="tag/node()"/> 

我輸入例如測試這一點,結果是示例的結果:

content 
<a> 
    b 
</a> 

沒有硬編碼根節點名稱,這可以是:

<xsl:copy-of select="./node()" /> 

當您已經在處理根節點並且想要內部所有元素(不包括根節點)的精確副本時,這非常有用。例如:

<xsl:variable name="head"> 
    <xsl:copy-of select="document('head.html')" /> 
</xsl:variable> 
<xsl:apply-templates select="$head" mode="head" /> 

<!-- ... later ... --> 

<xsl:template match="head" mode="head"> 
    <head> 
    <title>Title Tag</title> 
    <xsl:copy-of select="./node()" /> 
    </head> 
</xsl:template> 
3

補充Welbog的回答,其中有我的票,我建議寫獨立的模板,沿着這個線路:

<xsl:template match="/"> 
    <body> 
    <xsl:apply-templates select="description" /> 
    </body> 
</xsl:template> 

<xsl:template match="description"> 
    <div class="description"> 
    <xsl:copy-of select="node()" /> 
    </div> 
</xsl:template>