lbellonda / qxmledit

QXmlEdit XML editor. Downloads: https://sourceforge.net/projects/qxmledit/files
http://qxmledit.org
Other
160 stars 46 forks source link

Error in XPATH expression #90

Closed bdonauba closed 1 year ago

bdonauba commented 1 year ago

I have this little XPath expression that should work //stock[@pos=0]. Unfortunately it does not find the node in my XML document.

If I change the XPath expression to something like that //stock[posi_id=2144541] it even throws an error.

Here's the message:

Es ist nicht möglich, den Wert posi_id des Typs xs:string in xs:double zu wandeln : location: 2-15 http://www.w3.org/2005/xqt-errors#FORG0001

That means translated:

Is is not possible to convert the value of posi_id from string to double

Here is my test XML:


<?xml version="1.0" encoding="UTF-8"?>
<ausgaben>
  <daten>
    <stock>
      <posi_id pos="0">2144541</posi_id>
    </stock>
  </daten>
</ausgaben>

Regards, Bernhard Donaubauer

lbellonda commented 1 year ago

Hello, regarding the first query, the attribute pos is bound to the element "posi_id", so maybe the intended query was: //stock[./posi_id/@pos=0]

For the second one, there is a data mismatch, because the data in XML are not number, but text. You can use //stock[./posi_id/text()='2144541'] or //stock[number(./posi_id/text())=2144541]

In my opinion the behavior of QXmlEdit is correct.

Best regards.

bdonauba commented 1 year ago

Okay now that I know I can deal with it but I want to remark that all the tools (XSLT processor, Java XML Parser) I use and used do type conversion in XPATH automatically. That's astonishing to me that different XPATH implementations exist.

Check for example https://xslttest.appspot.com/

Enter the XML document:

<?xml version="1.0" encoding="UTF-8"?>
<ausgaben>
  <daten>
    <stock>
      <posi_id pos="0">2144541</posi_id>
    </stock>
  </daten>
</ausgaben>

Enter the transformation with the XPATH expression:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="//stock[posi_id=2144541]">
      <xsl:copy-of select="posi_id"/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each>
  </xsl:template> 
</xsl:stylesheet>

See the result: <posi_id pos="0">2144541</posi_id>

lbellonda commented 1 year ago

For XPath QXmlEdit is using Qt libraries as documented in Qt docs:
https://doc.qt.io/qt-5/xmlprocessing.html

bdonauba commented 1 year ago

Okay the difference is obviously Xpath 2.0 versus Xpath 1.0.

From https://en.wikipedia.org/wiki/XPath_2.0:

The main difference is that XPath 1.0 was more relaxed about type conversion, for example comparing two strings ("4" > "4.0") was quite possible but would do a numeric comparison; in XPath 2.0 this is defined to compare the two values as strings using a context-defined collating sequence.

Thank you for your support.