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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Analysis is on the mark, +1. Might want to add how not() can be used (albeit with an inefficiency that might matter on large datasets) to select maximums in XPath 1.0.
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)"/>

2 Comments

'max' is a XPATH 2.0 operator, right? I am using xsltproc which I believe do not support XPATH 2.0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.