【问题标题】:XML and XSLT - parameter value not in XMLXML 和 XSLT - 不在 XML 中的参数值
【发布时间】:2012-09-12 13:27:55
【问题描述】:

我有一个 XML 文件,其中包含多条记录和不同的值(例如包含名称、编号、重量等值的项目列表)。我使用 XSLT 在网页上以表格的形式显示这些内容。在每个单独的页面上都显示了不同的记录 - xslt 从网页中获取参数的值,然后仅显示适当的信息片段。例如,有一个关于项目 XYZ 的网页 - 仅显示该特定项目的名称、数量和重量。

我的问题是,当 XML 中没有此类项目时,是否有办法显示某种消息(例如“此项目没有可用数据”)。它不像参数为空或 null - 它仍然是从网页中获取的。只是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>

【问题讨论】:

  • 您能否展示您目前拥有的 XSLT 以及您作为输入的 XML 类型的示例?
  • 当然。我已经更改了参数/变量的名称和值,但是 XML 和 XSL 的构建方式与下面的完全一样。
  • 我知道 - 抱歉,不小心按了“输入”。它现在在问题描述中,因为评论太长了。

标签: xml xslt param


【解决方案1】:

除此之外

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

在 XSLT 1.0 中是不合法的(您不能在每个规范的匹配表达式中使用变量,但一些处理器无论如何都会接受它们),您的问题是模板仅在它们匹配的元素存在时才会触发。如果没有ItemRowitem 孩子是您要的那个,那么模板根本不会触发。

相反,您需要将“没有此项目的数据”逻辑上移到父 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>

【讨论】:

  • 谢谢!像魅力一样工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2017-10-04
  • 2010-10-30
相关资源
最近更新 更多