2012-07-18 21 views
0

之前,我有這樣的XML獲取一些元素項目的值與條件

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="do.xsl"?> 
<catalog> 
    <cd> 
    <title_>Empire Burlesque</title_> 
    <artist>Bobby</artist> 
    <company>Columbia</company> 
    </cd> 
    <cd> 
    <title_>Shirt2</title_> 
    <artist>Bobby</artist> 
    <company>Columbia2</company> 
    </cd> 
    <cd> 
    <title_>Fingers</title_> 
    <artist>Bobby</artist> 
    <company>Columbia3</company> 
    </cd> 
    <cd> 
    <title_>Zip1</title_> 
    <artist>Bobby</artist> 
    <company>---</company> 
    </cd> 
    <cd> 
    <title_>Zip2</title_> 
    <artist>Bobby</artist> 
    <company>---</company> 
    </cd> 
</catalog> 

我需要更換---與以前的有意義的數據 - Columbia3 我接下來做

<?xml version="1.0" encoding="utf-8"?> 

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/> 

<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
    <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
    </tr> 
    <xsl:for-each select="catalog/cd"> 
    <tr> 
     <td><xsl:value-of select="title_"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     <td> 
      <xsl:variable name="NotStarted" select="preceding-sibling::cd[1]/company" /> 
      <xsl:choose> 
      <xsl:when test="company != '---'"> 
       <xsl:value-of select="company"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy-of select="$NotStarted" /> 
      </xsl:otherwise> 
      </xsl:choose> 
     </td> 
    </tr> 
    </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

正如預期的那樣我得到了下一個HTML

Title Artist 
Empire Burlesque Bobby Columbia 
Shirt2 Bobby Columbia2 
Fingers Bobby Columbia3 
Zip1 Bobby Columbia3 
Zip2 Bobby --- 

我如何迭代得到最後有意義的字符串替換或任何o其他方式?謝謝。

+0

定義「有意義」。 – 2012-07-18 12:26:54

+1

@Dimitre Novatchev,我想這裏很清楚,不需要額外的解釋。我也認爲你太過於苛刻了)) – Yola 2012-07-18 13:48:22

+0

一點也不 - 你至少可以提供確切的想要的結果 - 如果即使缺少這個結果,人們也可以將「有意義」定義爲字符串「」有意義的「,並給你這個:'有意義的' – 2012-07-18 14:30:37

回答

1

這是可以通過改變來實現你的NotStarted變量

<xsl:variable name="NotStarted" select="preceding-sibling::cd[1]/company" /> 

什麼,這是目前在做的是找到不論內容直接前置兄弟,其公司元素得到。你需要做的是找到的第一個前置兄弟其中有一個有效的公司名稱

<xsl:variable name="NotStarted" 
    select="preceding-sibling::cd[company != '---'][1]/company/text()"/> 

值得注意的是,它通常是最好避免使用的xsl:對,每個的xsl:選擇在XSLT中使用,並嘗試利用模板匹配的功能。這裏是一個演示此

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/> 

    <xsl:template match="/"> 
     <html> 
     <body> 
      <h2>My CD Collection</h2> 
      <table border="1"> 
       <tr bgcolor="#9acd32"> 
        <th>Title</th> 
        <th>Artist</th> 
       </tr> 
       <xsl:apply-templates select="catalog/cd"/> 
      </table> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="cd"> 
     <tr> 
     <td> 
      <xsl:value-of select="title_"/> 
     </td> 
     <td> 
      <xsl:value-of select="artist"/> 
     </td> 
     <td> 
      <xsl:apply-templates select="company"/> 
     </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="company[text() = '---']"> 
     <xsl:value-of select="../preceding-sibling::cd[company != '---'][1]/company"/> 
    </xsl:template> 
</xsl:stylesheet> 

當施加到您的XML示例的替代XSLT,下面是輸出

<html> 
    <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
     </tr> 
     <tr> 
      <td>Empire Burlesque</td> 
      <td>Bobby</td> 
      <td>Columbia</td> 
     </tr> 
     <tr> 
      <td>Shirt2</td> 
      <td>Bobby</td> 
      <td>Columbia2</td> 
     </tr> 
     <tr> 
      <td>Fingers</td> 
      <td>Bobby</td> 
      <td>Columbia3</td> 
     </tr> 
     <tr> 
      <td>Zip1</td> 
      <td>Bobby</td> 
      <td>Columbia3</td> 
     </tr> 
     <tr> 
      <td>Zip2</td> 
      <td>Bobby</td> 
      <td>Columbia3</td> 
     </tr> 
     </table> 
    </body> 
</html> 
+0

很不錯的答案,即時通訊新的xsl,你是非常有幫助的。謝謝 – Yola 2012-07-18 13:51:13