2013-10-30 128 views
0

問題:XML將不會顯示標籤內的所有標籤

我們有一個顯示大量文本的XML文檔。該文本被包裝在-p標籤內,並且所有內容都被包裹在名爲-avsnitt-的標籤內。但是,每個-avsnitt-中只會顯示第一個-p標籤。

XML代碼:

<seksjon> 
<p>3.1. Introduction</p> 
<avsnitt> 
<p>SIMULA is a general purpose programming language. It inherits the algorithmic properties of ALGOL 60 and introduces methods for structuring data. The main characteristic of SIMULA is that it is easily modelled towards specialized problem areas, and hence can be used as a basis for Special Application Languages.</p> 

<p>In this Standard the name SIMULA is considered synonymous with SIMULA 67. Although there exists a predecessor, SIMULA I, this latter language has achieved limited use. It is recommended that the language defined in this Standard be referred to as "Standard SIMULA".</p> 

<p>SIMULA includes most of the ALGOL 60 language. Wherever ALGOL is used in this Standard it relates to the STANDARD ALGOL 60 definition (ISO 1538).</p> 
</avsnitt> 
</seksjon> 

XSL代碼:

<xsl:for-each select="kapittel/seksjon"> 
<h2><xsl:value-of select="p"/></h2> 
<br></br> 
<xsl:value-of select="avsnitt/p"/> 
</xsl:for-each> 

回答

1

以前的答案的確是最緊湊。爲了完整起見,我將提供一個通常在XML和樣式表變得越來越複雜時發現的詳細解決方案。

<xsl:template match="/parent-of-seksjons"> 
    <xsl:apply-templates select="seksjon"/> <!-- this was your xsl:for-each --> 
</xsl:template> 

<xsl:template match="seksjon"> 
    <xsl:apply-templates/> <!-- basically, filter the seksjon tag from output --> 
</xsl:template> 

<!-- (1) matches any p tag directly beneath seksjon --> 
<xsl:template match="seksjon/p"> 
    <!-- it's bad practice to <br/> just for whitespace --> 
    <h2 style="margin-bottom: 2em"><xsl:value-of select="."/></h2> 
</xsl:template> 

<xsl:template match="avsnitt"> 
    <xsl:apply-templates/> <!-- again, filter the tag but keep it's children --> 
</xsl:template> 

<!-- (2) matches any p tag directly beneath avsnitt --> 
<xsl:template match="avsnitt/p"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|*"/> 
    </xsl:copy> 
</xsl:template> 

<!-- catch-all --> 
<xsl:template match="@*|*" priority="-1"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

順便說一句,我不會用<seksjon><p><seksjon title=".."/>甚至<seksjon><title>如果可能的話。

如果這是太詳細,你可以添加模板(1)和模板(2)你的XSL,並與<xsl:apply-templates

3

這是正確的,如果你正在使用XSLT 1.0,因爲value-of給出包含多個節點將返回一個節點集時字符串值第一個節點集中的文檔順序。你可能想使用copy-of,而不是value-of,將所有選中的節點複製到結果樹

<xsl:for-each select="kapittel/seksjon"> 
<h2><xsl:value-of select="p"/></h2> 
<br></br> 
<xsl:copy-of select="avsnitt/p"/> 
</xsl:for-each> 

這將產生輸出像

<h2>3.1. Introduction</h2> 
<br /> 
<p>SIMULA is a general purpose programming language. It inherits the algorithmic properties of ALGOL 60 and introduces methods for structuring data. The main characteristic of SIMULA is that it is easily modelled towards specialized problem areas, and hence can be used as a basis for Special Application Languages.</p> 
<p>In this Standard the name SIMULA is considered synonymous with SIMULA 67. Although there exists a predecessor, SIMULA I, this latter language has achieved limited use. It is recommended that the language defined in this Standard be referred to as "Standard SIMULA".</p> 
<p>SIMULA includes most of the ALGOL 60 language. Wherever ALGOL is used in this Standard it relates to the STANDARD ALGOL 60 definition (ISO 1538).</p> 
+0

謝謝您更換<xsl:value-of!奇蹟般有效! – BTB