2013-10-30 2 views
0

문제 :XML에 태그 내의 모든 태그가 표시되지 않습니다

우리는 많은 텍스트를 보여주는 XML 문서를 가지고 있습니다. 이 텍스트는 -p-tags 안에 싸여 있으며 모든 것이 -avsnitt-라는 태그 안에 싸여 있습니다. 그러나 모든 -avsnitt-의 첫 번째 -p-tag 만 표시됩니다.

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 코드 : 반환됩니다 이상의 노드를 포함하는 노드 집합을 제공 value-of 때문에 XSLT 1.0을 사용하는 경우 올바른

<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

같은 결과 트리

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

에 선택한 모든 노드를 복사 할 대신 value-ofcopy-of를 사용하고 싶습니다. 완전성을 위해서 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> 

, BTW 나는 <seksjon><p>하지만 <seksjon title=".."/> 또는 <seksjon><title> 가능하면 사용하지 않을 것입니다. 이 너무 자세한 경우

, 당신은 단지 (1) 템플릿 (2) XSL에 템플릿을 추가하고 <xsl:apply-templates

3

, 문자열 순서 값 노드의 문자열 값입니다. 당신은 아마이 생산하는 것이다 출력 이전의 대답은 참으로 가장 컴팩트

<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

관련 문제