2012-09-12 17 views
2

我有一個XML文件,其中包含多個記錄和不同的值(如帶有諸如名稱,數字,重量等值的項目列表)。我使用XSLT在網頁上以表格的形式顯示這些內容。在每個頁面上顯示不同的記錄 - xslt從網頁獲取參數的值,然後僅顯示適當的信息。例如,有一個關於項目XYZ的網頁 - 只顯示該特定項目的名稱,編號和重量。XML和XSLT - 參數值不是XML

我的問題是當XML中沒有這樣的項目時,是否有方法顯示某種消息(如「沒有數據可用於此項目」)。這不像參數是空的或空的 - 它仍然來自網頁。只是在XML文件中沒有這樣的記錄。

請幫忙嗎?

您可以在下面找到XML和XLS文件的代碼。我已經更改了參數/變量名稱和值,但其他一切與我的原始文件保持一致。

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ItemsTable> 
<ItemRow> 
    <item>001</item> 
    <name>aaaa</name> 
    <price>2402</price> 
    <price2>2200</price2> 
</ItemRow> 
<ItemRow> 
    <item>002</item> 
    <name>bbbb</name> 
    <price>2402</price> 
    <price2>2700</price2> 
</ItemRow> 
<ItemRow> 
    <item>003</item> 
    <name>cccc</name> 
    <price>2402</price> 
    <price2>2003</price2> 
</ItemRow> 
<ItemRow> 
    <item>004</item> 
    <name>dddd</name> 
    <price>2402</price> 
    <price2>2024</price2> 
</ItemRow> 

XSL:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:param name="thisitem">XXXX</xsl:param> 
<xsl:template match="/"> 
<xsl:apply-templates /> 
</xsl:template> 
<xsl:decimal-format name="european" decimal-separator="," grouping-separator="." /> 
<xsl:template match="/ItemsTable/ItemRow" /> 
<xsl:template match="/ItemsTable/ItemRow [ item = $thisitem ]"> 
<style>table.YYY { border-collapse: collapse; } table.YYY td, table.YYY th { border: 1px solid black; padding: 1em; vertical-align: middle; text-align: center; } table.YYY th { background-color: #eee; } table.YYY .header { font-size: 2em; font-weight: bold; padding-bottom: 1em; padding-top: 1em; } table.YYY .itemname { color: red; font-weight: bold; white-space: nowrap; } table.YYY .yellow { background-color: yellow; } table.YYY .red { background-color: red; } table.YYY .green { background-color: #40FF00; }</style> 
<html> 
<body> 
<table class="YYY"> 
<xsl:if test="string-length(name) > 0 and string-length(price) > 0 and string-length(price2) > 0" /> 
<xsl:choose> 
<xsl:when test="string-length(name) > 0 and string-length(price) > 0 and string-length(price2) > 0"> 
<tr> 
<th>Name</th> 
<th>Price 1</th> 
<th>Price 2</th> 
</tr> 
<tr> 
<xsl:choose> 
<xsl:when test="price > price2"> 
<td class="red"> 
<xsl:value-of select="name" /> 
</td> 
<td class="red"> 
<xsl:value-of select="format-number(price, '###.###.###', 'european')" /> 
</td> 
<td class="red"> 
<xsl:value-of select="format-number(price2, '###.###.###', 'european')" /> 
</td> 
</xsl:when> 
<xsl:otherwise> 
<td class="green"> 
    <xsl:value-of select="name" /> 
</td> 
<td class="green"> 
    <xsl:value-of select="format-number(price, '###.###.###', 'european')" /> 
</td> 
<td class="green"> 
    <xsl:value-of select="format-number(price2, '###.###.###', 'european')" /> 
</td> 
</xsl:otherwise> 
</xsl:choose> 
</tr> 
</xsl:when> 
<xsl:otherwise> 
<div> 
    <p>No data for this item</p> 
</div> 
</xsl:otherwise> 
</xsl:choose> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 
+2

你能告訴你有這麼遠的XSLT和一種XML的例子你有輸入? –

+0

當然。我已經更改了參數/變量名稱和值,但XML和XSL的構建方式與下面的完全相同。 – Marta

+0

沒有什麼「下」 –

回答

0
的事實,

<xsl:template match="/ItemsTable/ItemRow [ item = $thisitem ]"> 

是不是在XSLT 1.0法律

除了(你不能使用每個規範的匹配表達式中的變量但有些處理器無論如何都接受它們),這裏的問題在於模板只有在匹配的元素存在時纔會觸發。如果沒有ItemRow其子item孩子是你要求的那個,那麼模板根本不會開火。

相反,你需要移動的「無數據爲這個項目」邏輯成一個模板父ItemsTable

<xsl:template match="/ItemsTable"> 
    <xsl:variable name="matchingRows" select="ItemRow[item = $thisitem][string-length(name)][string-length(price)][string-length(price2)]"/> 
    <xsl:choose> 
    <xsl:when test="$matchingRows"> 
     <xsl:apply-templates select="$matchingRows"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <div> 
     <p>No data for this item</p> 
     </div> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template match="ItemRow"> 
    <!-- logic for each matching row here, don't need to check the 
     preconditions as the template is only called for rows that match --> 
</xsl:template> 
+0

謝謝!奇蹟般有效 :) – Marta