mavrosxristoforos / get-xml-info

Get Information from XML files to use into your GitHub workflows
GNU General Public License v3.0
6 stars 13 forks source link

XPath didnt return any node #6

Closed danielferromeral closed 2 years ago

danielferromeral commented 2 years ago

Hello.

I'm trying to parse a pom.xml to get the version. The pom looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4</version>
    <relativePath/>
  </parent>
  <version>1.3.2</version>
 ........
</project>

And the action looks like this:

 - name: Get XML
        id: xml
        uses: mavrosxristoforos/get-xml-info@1.0
        with:
          xml-file: 'pom.xml'
          xpath: '//project/version'

And everytime i get this error:

File was read successfully. Proceeding to parse DOM. Found 0 nodes. Error: Your xpath did not return any nodes.

I have tried with //project, //version, //project/version/text(), but all the time I get the same error. Any tips on how to solve it?

Thanks in advance.

mavrosxristoforos commented 2 years ago

Hi there. Do you need the modelVersion or the parent version? The //version xpath should have done it for the parent version, but you should also try //project/parent/version. If you want the modelVersion, you should try //modelVersion.

danielferromeral commented 2 years ago

Hello, no, I want the version of the project, not the child of the parent, the 1.3.2.

mavrosxristoforos commented 2 years ago

Did you try to write //modelVersion? What does it do?

danielferromeral commented 2 years ago

Same result:

XPath: //modelVersion File was read successfully. Proceeding to parse DOM. Found 0 nodes. Error: Your xpath did not return any nodes.

mavrosxristoforos commented 2 years ago

Alright. I found it. This happens because the POM xml has a namespace, and XPath doesn't work exactly the same with namespaces. Here's the XPath you can write to get the project version: //*[local-name()='project']/*[local-name()='version']

danielferromeral commented 2 years ago

Yeah, that works. Lots of thanks.

pekspro commented 1 year ago

I hade a similar issue. I wanted to read the attribute:

/Package/Identity/@Version

But due to namespaces, I needed to use this query instead:

/*[local-name()='Package']/*[local-name()='Identity']/@Version

Maybe someone finds this useful :-)