2009-09-18 58 views
0

我有下面的XML文件:如何在變體定義標籤

<?xml version="1.0"?> 
<Table69> 
</Table69> 

而且要讀元素「Table69」的身體,我已經使用以下XSL文件:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml"/> 
    <xsl:template match="/">  
    <xsl:variable name="table" >  
     <xsl:choose> 
     <xsl:when test="normalize-space(.) != ''" > 
      <xsl:value-of select="." /> 
     </xsl:when> 
     <xsl:otherwise> 
      <Exception> 
       field was missing 
      </Exception>     
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <Table id="69"> 
     <xsl:value-of select="$table" /> 
    </Table> 

    </xsl:template> 
</xsl:stylesheet> 

問題是:當xml文件包含Table69 tage下的值時,此值會成功打印,但是; 當此標記不包含一個值,在xsl vairalbe「表」應包含以下內容:

<Exception> 
    field was missing 
</Exception>  

但是,它不包括踏歌,這裏是改造的樣本結果:

<?xml version="1.0"?> 
<Table id="69"> 
    field was missing  <!-- where's the Exception tag surrounding the text field was missing ??? --> 
</Table> 

回答

3

xsl:value-of將打印選定內容的文本值,使用xsl:copy-of輸出包括任何節點的整個內容。

<xsl:copy-of select="$table" /> 
0

請不要使用以下的<Exception>

<xsl:element name="Exception"> 
    <xsl:text>Field was missing</xsl:text> 
    </xsl:element> 

這應該生成你的<Exception> XML元素。 如果您需要圍繞文本的換行符,請刪除<xsl:text>標記。

+1

不幸的是,value-of仍將輸出將省略節點的文本值。 – Thiyagaraj