使用 XSLT 和 XPath 计算元素最大属性值失败 [英] Calculating element max attribute value fails using XSLT and XPath

查看:21
本文介绍了使用 XSLT 和 XPath 计算元素最大属性值失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 ui.xsl 以将 ui.xml 转换为 ui.html.ui.xml 是:

I am coding a ui.xsl to translate ui.xml into ui.html. The ui.xml is:

<?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 是:

My ui.xsl is:

<?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";但是,结果如下:

I am hoping that I can see the ui.html has "maxRow = 11" and "maxCol = 12"; However, here is the result:

<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?代码逻辑一样,只是两个不同的属性.

So my question here is what's wrong here? Why I can get maxRow correctly but not for maxCol? The code logic is the same, just two different attributes.

推荐答案

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

So my question here is what's wrong here?

您的测试与您认为的不同:

Your test does not do what you think it does:

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

对于任何 @row 大于或等于至少一个 其前面的兄弟时返回真.这意味着它适用于许多值,而不仅仅是最大的值.

will return true for any @row that is greater than or equal to at least one of its preceding siblings. That means it will be true for many values, not only the largest one/s.

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

And since you're obviously using an XSLT 1.0 processor, despite your stylesheet being tagged with version="2.0", you will get the first value that meets this condition, i.e. the value from the 2nd item.

您获得 @row 的预期结果纯属巧合.

The fact that you are getting the expected result for the @row is pure coincidence.

这篇关于使用 XSLT 和 XPath 计算元素最大属性值失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆