2

いくつかのレコードと異なる値(名前、番号、重みなどの値を持つアイテムのリストなど)を含む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>
4

1 に答える 1

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>
于 2012-09-12T14:42:01.843 に答える