2012-01-04 44 views
1

我敢打賭,這是一個簡單的問題。元素丟失時輸出文字

我試圖使用XSL設計我的xml文件。我試圖做的事情是,當我的XML文件中的元素丟失時,我想讓xsl輸出「Missin元素」

我設法創建一個字符串,表示元素中沒有數據,但那是什麼時候我沒有元素中的任何數據,但元素仍然存在。

我貼我的XML文件,以更好地解釋

<autoads> 
    <ad> 
     <type>1</type> 
     <name>Honda</name> 
     <model>XL 1000 V</model> 
     <regyear>2001</regyear> 
     <price>129900</price> 
     <adtext>2001 Honda XL 1000 V, 8.900 km. hög vindruta. Pris 129.900kr,-. </adtext> 
     <addate>20020115</addate> 
     <volume>1000</volume> 

    </ad> 
    <ad> 
     <type>2</type> 
     <name>Nissan</name> 
     <model>Almera 1.4S</model> 
     <regyear>1997</regyear> 
     <price>119000</price> 
     <adtext>1997 Nissan Almera 1.4S, 5 dörrar, met, 70.000 km. el.spegel/fönster, galv. kaross, c.lås, startspärr, airbag, nedfällb. baks. ABS, ute temp. R/CD, alarm, d.fäste, v.säten, s/v-hj. EU-godk. full service, servo. Pris 119.000 kr,-. </adtext> 
     <addate>20020118</addate> 
     <volume>0</volume> 
     <category>5 dörrar</category> 
    </ad> 
</autoads> 

正如你瑟元素類別中的第一個失蹤,

那它應該是什麼我嘗試輸出與一個XSL字符串,打印出「類別丟失」

感謝您的一切幫助。

回答

1

您需要指定您期待的元素並測試它們的存在。在最簡單的情況下,使用這樣的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="ad"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
      <xsl:if test="not(category)"> 
       <xsl:comment>category is missing</xsl:comment> 
      </xsl:if> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

這地方丟失的節點的輸出評論,但你可以很容易地修改它輸出的元素或簡單的文本。

爲了更完整的解決方案,創建所需的元素的列表,並使用document函數來驗證此列表中的每個項目的存在:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:m="m"> 
    <xsl:output method="xml" indent="yes"/> 
    <m:req>type</m:req> 
    <m:req>name</m:req> 
    <m:req>model</m:req> 
    <m:req>regyear</m:req> 
    <m:req>price</m:req> 
    <m:req>adtext</m:req> 
    <m:req>addate</m:req> 
    <m:req>volume</m:req> 
    <m:req>category</m:req> 
    <xsl:variable name="required" select="document('')/*/m:req"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="ad"> 
     <xsl:variable name="this" select="."/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:for-each select="$required"> 
       <xsl:variable name="search" 
        select="$this/*[name()=current()]"/> 
       <xsl:if test="$search"> 
        <xsl:apply-templates select="$search"/> 
       </xsl:if> 
       <xsl:if test="not($search)"> 
        <xsl:comment> 
         <xsl:value-of select="concat(., ' is missing')"/> 
        </xsl:comment> 
       </xsl:if> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

太感謝你了!在我的選擇元素中使用它,它的工作就像我想:) – Dymond 2012-01-04 21:35:19

+0

@FelipeOtarola - 沒問題。爲了好玩,我已經包含了另一個更完整的解決方案。看我的編輯。 – 2012-01-04 21:36:09

+0

這樣的絕地,你是:)。我將閱讀關於文檔功能,我的解決方案現在更簡單,因爲我剛開始使用XML課程。 再次感謝。 – Dymond 2012-01-04 21:47:00