【问题标题】:Calculating element max attribute value fails using XSLT and XPath使用 XSLT 和 XPath 计算元素最大属性值失败
【发布时间】:2020-10-26 12:55:23
【问题描述】:

我正在编写一个 ui.xsl 来将 ui.xml 翻译成 ui.html。 ui.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
  <layout class="QGridLayout" name="gridLayout_2">

     <item row="8" column="1" colspan="12">
     </item>
     <item row="11" column="1" colspan="2">
     </item>
     <item row="11" column="3" colspan="2">
     </item>
     <item row="11" column="5" colspan="2">
     </item>
     <item row="11" column="7" colspan="3">
     </item>
     <item row="1" column="6" colspan="3">
     </item>
     <item row="2" column="1" colspan="3">
     </item>
     <item row="2" column="0">
     </item>
     <item row="3" column="1" colspan="3">
     </item>
     <item row="6" column="1">
     </item>
     <item row="6" column="2" colspan="4">
     </item>
     <item row="3" column="9">
     </item>
     <item row="7" column="0">
     </item>
     <item row="7" column="1" colspan="12">
     </item>
     <item row="3" column="12">
     </item>
     <item row="4" column="0">
     </item>
     <item row="4" column="6" colspan="3">
     </item>
     <item row="4" column="9">
     </item>
     <item row="4" column="10">
     </item>
     <item row="10" column="1" colspan="12">
     </item>
     <item row="4" column="11">
     </item>
     <item row="4" column="12">
     </item>
     <item row="5" column="0">
     </item>
     <item row="5" column="1" colspan="3">
     </item>
     <item row="5" column="6" colspan="2">
     </item>
     <item row="5" column="9">
     </item>
     <item row="5" column="11">
     </item>
     <item row="5" column="12">
     </item>
     <item row="1" column="0">
     </item>
     <item row="4" column="1" colspan="3">
     </item>
     <item row="4" column="4">
     </item>
     <item row="2" column="12">
     </item>
     <item row="3" column="0">
     </item>
     <item row="1" column="1" colspan="3">
     </item>
     <item row="9" column="1" colspan="12">
     </item>
     <item row="1" column="9">
     </item>
     <item row="3" column="6" colspan="3">
     </item>
     <item row="1" column="11">
     </item>
     <item row="1" column="12">
     </item>
     <item row="3" column="11">
     </item>
     <item row="2" column="11">
     </item>
     <item row="0" column="8">
     </item>
  </layout>
</ui>

我的 ui.xsl 是:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="html" />

  <xsl:variable name="maxRow" select="/ui/layout/item
                                            [
                                              (@row >= preceding-sibling::item/@row) and
                                              (@row >= following-sibling::item/@row)
                                            ]/@row" />

  <xsl:variable name="maxCol" select="/ui/layout/item
                                            [
                                              (@column >= preceding-sibling::item/@column) and
                                              (@column >= following-sibling::item/@column)
                                            ]/@column" />

  <xsl:template match="/">
    <html>
      <head>
        <title>
        </title>
      </head>
      <body>
        <p>
          maxRow = <xsl:value-of select="$maxRow"/>
          maxCol = <xsl:value-of select="$maxCol"/>
        </p>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

我希望我能看到 ui.html 有“maxRow = 11”和“maxCol = 12”;但是,结果如下:

<html xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body><p>
          maxRow = 11
          maxCol = 1</p></body>
</html>

所以我的问题是这里有什么问题?为什么我可以正确获取 maxRow 而不是 maxCol?代码逻辑是一样的,只是两个不同的属性。

【问题讨论】:

    标签: html xml xslt xpath


    【解决方案1】:

    所以我的问题是这里有什么问题?

    您的测试没有按照您的想法进行:

    @row >= preceding-sibling::item/@row
    

    将为 any @row 返回 true,该 @row 大于或等于 至少一个 其前面的同级。这意味着它适用于许多值,而不仅仅是最大的值。

    而且由于您显然使用的是 XSLT 1.0 处理器,尽管您的样式表被标记为 version="2.0",您将获得第一个满足此条件的值,即来自第二个 item 的值。

    您得到@row 的预期结果纯属巧合。

    【讨论】:

    • 分析成功,+1。可能需要添加 not() can be used(尽管效率低下可能对大型数据集很重要)在 XPath 1.0 中选择最大值的方法。
    【解决方案2】:

    你不能像这样简单地做吗:

    <xsl:variable name="maxRow" select="max(ui/layout/item/@row)"/>
    
    <xsl:variable name="maxCol" select="max(ui/layout/item/@column)"/>
    

    【讨论】:

    • 'max' 是 XPATH 2.0 运算符,对吧?我正在使用我认为不支持 XPATH 2.0 的 xsltproc
    • 是的。当您提出问题时,请指明您使用的是哪个 XSLT 引擎。您的 XSLT 转换代码还显示:样式表版本 =“2.0”。由于您使用的是 XSLT 1.0 引擎,因此您应该更改它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 2012-09-30
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多