Calculating element max attribute value fails using XSLT and XPath

余生颓废 提交于 2021-01-28 08:01:35

问题


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>

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>

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>

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.


回答1:


So my question here is what's wrong here?

Your test does not do what you think it does:

@row >= preceding-sibling::item/@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.

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.

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




回答2:


Can't you do it simply like this :

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

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


来源:https://stackoverflow.com/questions/64537533/calculating-element-max-attribute-value-fails-using-xslt-and-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!