2014-02-27 155 views
1

無條件地在哪裏我會寫,說,有條件布爾屬性添加標籤使用XSLT

<season boolean:warm="true"> 

我會通過XSLT有條件的屬性集,但我不知道是幹什麼用的語法

<season> 
     <xsl:if ...> 
     <xsl:attribute name="warm"> 
     true 
     </xsl:attribute> 
     </xsl:if> 
    </season> 

使用此xslt結果的程序無法處理此變體

+0

是與命名空間問題,也許?您的條件屬性不是使用名稱空間創建的。也許你可以顯示「布爾」前綴的名稱空間?謝謝! –

回答

0

如果要在XSLT中使用名稱空間,則必須聲明它們。讓我們假設你的來源是這樣的:

<months> 
    <month>January</month> 
    <month>August</month> 
</months> 

既然你需要在你的屬性前綴,你必須聲明的命名空間。您沒有顯示名稱空間,所以我將使用隨機字符串。下面的XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:boolean="put-your-namespace-here"> 

    <xsl:template match="months"> 
     <seasons><xsl:apply-templates/></seasons> 
    </xsl:template> 
    <xsl:template match="month"> 
     <season> 
      <xsl:if test="text() = 'August'"> 
       <xsl:attribute name="boolean:warm">true</xsl:attribute> 
      </xsl:if> 
      <xsl:value-of select="."/> 
     </season> 
    </xsl:template> 
</xsl:stylesheet> 

將產生以下結果:

<seasons xmlns:boolean="put-your-namespace-here"> 
    <season>January</season> 
    <season boolean:warm="true">August</season> 
</seasons>