1

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Max Li
  • 551
  • 2
  • 14

2 Answers2

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Analysis is on the mark, +1. Might want to add how `not()` [can be used](https://stackoverflow.com/q/8702039/290085) (albeit with an inefficiency that might matter on large datasets) to select maximums in XPath 1.0. – kjhughes Oct 26 '20 at 13:44
0

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)"/>
Sebastien
  • 2,672
  • 1
  • 8
  • 13
  • 'max' is a XPATH 2.0 operator, right? I am using xsltproc which I believe do not support XPATH 2.0 – Max Li Oct 27 '20 at 02:24
  • Yes. When you ask a question, please indicate which XSLT engine you are using. Also your XSLT transformation code says : stylesheet version="2.0". Since you are using an XSLT 1.0 engine you should change that. – Sebastien Oct 27 '20 at 13:37