2016-03-31 47 views
-2

我的XML是:如何打印父節點的文本和前兩個孩子

<content> 
    <p>The <organization>Committee on Civil Liberties</organization> calls on the <organization>Committee on Foreign Affairs</organization>, as the committee responsible: 
    <mod> 
     <quotedStructure> 
     <recital id="_900411"> 
       <num>A.</num> 
       <p>Whereas the progressive development ...</p> 
      </recital> 
      <recital id="_900412"> 
       <num>B.</num> 
       <p>Whereas Article ...</p> 
      </recital> 
      <recital id="_900413"> 
       <num>C.</num> 
       <p>Whereas, while Member States ...</p> 
      </recital> 
     </quotedStructure> 
    </mod> 
</p> 

我的預期成果是:

委員會對公民自由的呼籲外交委員會,作爲委員會負責人:

A.儘管逐步發展....

B.雖然第...

C.然而,雖然成員國...

我的XSLT是:

<xsl:template match="content/p"> 
<xsl:value-of select="text()"/> 
<xsl:apply-templates/> 
</xsl:template> 
<xsl:template match="content//recital"> 
    <xsl:apply-templates/> 
</xsl:template> 
<xsl:template match="content//recital/num"> 
    <xsl:value-of select="."/> 
</xsl:template> 
<xsl:template match="content//recital/p"> 
    <xsl:value-of select="." /> 
</xsl:template> 

我的電流輸出是:

作爲委員會負責人的電話:

A.而逐步發展......

B.雖然第...

C.然而,雖然成員國...

注:我很想念裏面的文字<organization>父節點<p>節點的子節點。 請讓我知道XPath和XSLT以獲得預期的輸出。

+0

請出示至少冷杉嘗試。並添加您所需的輸出 - 而不是圖像。 –

+1

您是否可以顯示一個完整的XSLT來演示問題,因爲子節點「組織」實際上將由XSLT的默認模板代替。 (例如,請參閱http://xsltransform.net/gWvjQgg)。謝謝! –

+0

您的XML格式不正確。如果您希望得到答案,請糾正。 – zx485

回答

0

使用這種(固定)XML文件

<content> 
    <p>The <organization>Committee on Civil Liberties</organization> calls on the <organization>Committee on Foreign Affairs</organization>, as the committee responsible: 
     <mod > 
      <quotedStructure> 
       <recital id="_900411"> 
        <num>A.</num> 
        <p>Whereas the progressive development ...</p> 
       </recital> 
       <recital id="_900412"> 
        <num>B.</num> 
        <p>Whereas Article ...</p> 
       </recital> 
       <recital id="_900413"> 
        <num>C.</num> 
        <p>Whereas, while Member States ...</p> 
       </recital> 
      </quotedStructure> 
     </mod> 
    </p> 
</content> 

這個XSLT文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:strip-space elements="p" /> 

    <xsl:template match="content"> 
    <xsl:apply-templates select="*"/> 
    </xsl:template> 

    <xsl:template match="p/text()"> 
    <xsl:for-each select="."> 
     <xsl:value-of select="." /> 
    </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="mod/quotedStructure"> 
    <xsl:for-each select="recital"> 
     <xsl:text>&#10;</xsl:text> 
     <xsl:value-of select="concat(num/text(),' ')" /> 
     <xsl:value-of select="p/text()" /> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

的結果是這樣的:

The Committee on Civil Liberties calls on the Committee on Foreign Affairs, as the committee responsible: 


A. Whereas the progressive development ... 
B. Whereas Article ... 
C. Whereas, while Member States ... 
相關問題