2010-11-10 88 views
26

在運行時在一個XML文件中存在的屬性,我可以有一個XML文件的兩種格式:如何檢查是否使用XSL

  1. <root> 
        <diagram> 
         <graph color= "#ff00ff">  
          <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> 
          <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> 
         </graph> 
        </diagram> 
    </root> 
    
  2. <root> 
        <diagram> 
         <graph>  
          <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> 
          <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> 
         </graph> 
        </diagram> 
    </root> 
    

根據的存在顏色屬性 我必須處理xaxis和yaxis的值。

我需要使用XSL來做到這一點。 任何人都可以幫助我暗示我一個片段,我可以檢查這些條件。

我嘗試使用

<xsl: when test="graph[1]/@color"> 
    //some processing here using graph[1]/@color values 
</xsl:when> 

我得到一個錯誤......

+0

你得到什麼錯誤? – 2010-11-10 16:33:28

+0

您是否使用過'xsl:choose' ::('xsl:when' + |'xsl:otherwise')指令? – 2010-11-10 16:37:54

+0

是的,我使用了xsl:choose ... xsl:when + xsl:否則條件是徒勞的。 – srivatsa 2010-11-10 16:41:27

回答

1

我不明白這一點 - 從一些輕微的語法除了對使用應用模板調整:

<xsl:template match="graph[1][@color]"> 
    <!-- your processing here --> 
</xsl:template> 

沒有多少我們可以告訴你,不知道你真的想做什麼。

+0

我只是想根據xml文件中屬性「color」的存在來處理圖... – srivatsa 2010-11-10 16:46:45

+0

它的like if(屬性存在嗎?)process 1;其他過程2; – srivatsa 2010-11-10 16:48:30

+0

此圖[1] [@color]不識別屬性存在 – srivatsa 2010-11-10 17:24:23

4
<xsl:when test="graph[1]/@color"> 
    //some processing here using graph[1]/@color values 
</xsl:when> 

我打算做一個猜測,因爲在這裏你的問題是缺少了很多重要的信息,如上下文中thie <xsl:when...出現。如果你的評論是正確的,你想要做的是處理graph[1]/xaxis.../yaxis,而不是graph[1]/@color值。

+0

是的。例如。基於屬性「顏色」的存在,我需要設置一些變量say..graph-color。現在我想要一個簡單的if-else語句根據屬性是否存在。如果爲true ..那麼我可以設置一個變量包含在屬性中。否則我可能會使用默認顏色 – 2010-11-10 22:02:07

+0

請修改您的帖子以包含更多樣式表,以便我們能夠弄清楚您要完成的任務。 – 2010-11-10 22:38:10

30

這是一個非常簡單的方式做條件處理使用XSLT模式匹配的強大功能和專門的「推」的風格,而這甚至避免了需要使用條件的說明,如<xsl:if><xsl:choose>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/root/diagram[graph[1]/@color]"> 
    Graph[1] has color 
</xsl:template> 

<xsl:template match="/root/diagram[not(graph[1]/@color)]"> 
    Graph[1] has not color 
</xsl:template> 
</xsl:stylesheet> 

當該變換被應用在下面的XML文檔

<root> 
    <diagram> 
     <graph color= "#ff00ff"> 
      <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> 
      <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> 
     </graph> 
     <graph> 
      <xaxis>101 102 103 1012 10312 103123 1</xaxis> 
      <yaxis>101 102 103 1012 10312 103123 1</yaxis> 
     </graph> 
    </diagram> 
</root> 

的希望,正確的結果產生

Graph[1] has color 

當相同的變換應用此XML文檔上:

<root> 
    <diagram> 
     <graph> 
      <xaxis>101 102 103 1012 10312 103123 1</xaxis> 
      <yaxis>101 102 103 1012 10312 103123 1</yaxis> 
     </graph> 
     <graph color= "#ff00ff"> 
      <xaxis>1 2 3 12 312 3123 1231 23 </xaxis> 
      <yaxis>1 2 3 12 312 3123 1231 23 </yaxis> 
     </graph> 
    </diagram> 
</root> 

再次想和正確的結果產生

Graph[1] has not color 

可以自定義此解決方案,並將第一個模板中所需的任何代碼以及第二個模板中的必要代碼放入。

+1

對於XSLT樣式爲+1。 – Flack 2011-02-24 07:19:59

+0

@Dimitre Novatchev是否可以將此解決方案用於<應用程序模板>說明中?我使用XSLT從XML生成代碼,我想過濾一些getter和setter? – chepseskaf 2011-10-11 09:29:37

+0

@chepseskaf:如果你的意思是有可能使用的謂詞在'XSL的'select'屬性:改爲適用-templates'在匹配模式有他們,答案是肯定的,但這樣做是沒有必要的,導致更加「推動」或必然的處理風格,這會降低簡單性,可理解性,可維護性和優化性。 – 2011-10-11 12:39:27

16

自定義模板,在一場比賽這樣

<xsl:template match="diagram/graph"> 
    <xsl:choose> 
    <xsl:when test="@color"> 
     Do the Task 
    </xsl:when> 
    <xsl:otherwise> 
     Do the Task 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template>** 
+0

在xsl函數中有用 – Charles 2013-02-04 14:11:09

+0

感謝您的回答,它對我有用。 – 2013-08-06 06:32:36