2014-01-30 12 views
1

我寫的XSLT代碼格式化sample.xml中(見下文)如何在xslt中使用「if」來表示角色?

我所試圖做的是,在我的Sample.xslt文件,我檢查用於type = MP4和顯示名稱(在這種情況下,應顯示的名稱是RequiredFileName。總之,我想,當我將XSLT我的XML文件,我讓我的MP4文件的名稱。

sample.xml中

<assets> 

<asset id="01" path="//my_computer/path" name="testfile" type="mov"> 
<child_entries> 

<asset id="21" path="//my_computer/test.mp3" name="001-2323" type="mp3"></asset> 
<asset id="23" path="//my_computer/test.mp4" name="RequiredFileName" type="mp4"></asset> 
<child_entries> 

</assets> 

樣品.xslt

<?xml version='1.0' ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 

    <Path> 
<xsl:for-each select="assets"/> 
<xsl:if test="/asset/child_entries/asset/@type = mp4"> 
<xsl:value-of select="child_entries/asset/@name"/> 
</xsl:if> 
    </Path> 

</xsl:template> 

謝謝你的閱讀扔它。

回答

0

你在上面的xml中有幾個錯別字。修復了諸如非封閉元素之類的拼寫錯誤之後,我只是添加了一些額外的嵌套循環。當我看到@lwburk已經顯示時,你可以大大簡化這一點。這裏是一個工作的樣式表:

<?xml version='1.0' ?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
      <xsl:for-each select="assets"> 
       <xsl:for-each select="asset"> 
        <xsl:for-each select="child_entries"> 
         <Path><xsl:value-of select="asset[@type='mp4']/@path" /></Path> 
        </xsl:for-each> 
       </xsl:for-each> 
      </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

,這裏是一個工作的XML輸入文件:

<assets> 
    <asset id="01" path="//my_computer/path" name="testfile" type="mov"> 
     <child_entries> 
      <asset id="21" path="//my_computer/test.mp3" name="001-2323" 
       type="mp3" ></asset> 
      <asset id="23" path="//my_computer/test.mp4" name="RequiredFileName" 
       type="mp4" ></asset> 
     </child_entries> 
    </asset> 
</assets> 

這些輸出:

<?xml version="1.0" encoding="UTF-8"?><Path>//my_computer/test.mp4</Path> 

我選擇路徑,因爲你已經包括Path元素。如果你只是想名字,你可以替換該行的XSLT文件:

<Path><xsl:value-of select="asset[@type='mp4']/@path" /></Path> 

有:

<xsl:value-of select="asset[@type='mp4']/@name" /> 
+1

想提一提的是,@lwburk解決方案是更好的,只是想提供一個解決方案,更接近於「爲你的代碼工作」。祝你好運! –

+0

謝謝,循環解決了這個問題 – Omer

2

你認爲你的問題是關於xsl:if但它確實不是。這可以更直接地完成。請看下面的樣式表:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <Path> 
      <xsl:apply-templates select="//asset[@type='mp4']/@path"/> 
     </Path> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<Path>//my_computer/test.mp4</Path> 

請注意,您原來的輸入是沒有很好地形成,所以不是做出關於如何解決它,我使用的是descendant-or-self軸假設(//)在任何級別選擇asset元素。如果您確切知道此元素將在文檔中出現的位置,則有更高效的解決方案。

讓我們打破了這個最重要的一塊下來(//asset[@type='mp4']/@path):

  • //asset選擇文檔中的所有asset元素(//是短期的/descendant-or-self::node()/

  • [@type='mp4']濾波器這份名單包括只有asset元素的屬性名稱爲type其字符串值爲mp4

  • /@path選擇每個節點的path屬性在上一步驟

沒有其他節點選擇返回,默認模板打印柄文,所以這真的是你所需要的。

請注意,如果您有多個此類型的元素,則可能無法打印。你想最終是這樣的:

<Path>//my_computer/test.mp3//my_computer/test.mp4</Path> 

如果你想在輸入輸出每個匹配元素的一個Path元素,然後做這樣的事情:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="//asset[@type='mp4']" /> 
    </xsl:template> 
    <xsl:template match="asset"> 
     <Path> 
      <xsl:value-of select="@path" /> 
     </Path> 
    </xsl:template> 
</xsl:stylesheet> 

像往常一樣,更具體的產出取決於一組可能的投入和你的特殊口味。

作爲一個閉注,幾乎從來沒有必要使用xsl:for-each。 XSLT處理器已經遍歷文檔中的節點。這基本上是XSLT工作原理的核心。最好直接匹配/選擇你想要的東西。

相關問題