いくつかのレコードと異なる値(名前、番号、重みなどの値を持つアイテムのリストなど)を含むXMLファイルがあります。XSLTを使用して、それらをWebページ上の表の形式で表示します。すべてのページに異なるレコードが表示されます。xsltはWebページからパラメーターの値を取得し、適切な情報のみを表示します。たとえば、アイテムXYZに関するWebページがあります。この特定のアイテムの名前、数、および重量のみが表示されます。
私の質問は、XMLにそのようなアイテムがない場合に、ある種のメッセージ(「このアイテムのデータはありません」など)を表示する方法があるかどうかです。パラメータが空またはnullのようではなく、Webページから取得されます。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>