check values of 2 XPath-Expressions

Here should go questions about transforming XML with XSLT and FOP.
poroton
Posts: 4
Joined: Thu Aug 09, 2018 3:29 pm

check values of 2 XPath-Expressions

Post by poroton »

Hi,

I want to test if values of nodes are the same:

Code: Select all


<xsl:if test="'//ab:person1/ab:gender' and '//ab:person2/ab:gender' '">
Why isn't this working?
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: check values of 2 XPath-Expressions

Post by Radu »

Hi,

You usually need to use the "=" sign to compare two nodes or strings in XSLT. But there is not much context in what you posted to help further.
Usually when you post about an XSLT problem, posting a small sample XML and XSLT stylesheet help others understand better what you want to do.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
poroton
Posts: 4
Joined: Thu Aug 09, 2018 3:29 pm

Re: check values of 2 XPath-Expressions

Post by poroton »

I changed my example to following (working) problem case:


Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ab="http://ab.example.net/V1.0/Schema" exclude-result-prefixes="ab">
<xsl:template match="/">
<Data>
<xsl:if test="'//ab:kov-data/ab:person1/ab:gender' = '//ab:kov-data/ab:person2/ab:gender'">
:)
</xsl:if>
</Data>
</xsl:template>
</xsl:stylesheet>

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<ab:kov-data xmlns:ab="http://ab.example.net/V1.0/Schema">
<ab:person1>
<ab:gender>m</ab:gender>
</ab:person1>
<ab:person2>
<ab:gender>m</ab:gender>
</ab:person2>
</ab:kov-data>
The result should be:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><Data>:)</Data>
Using "=" didn't worked :/
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: check values of 2 XPath-Expressions

Post by Radu »

Hi,

If you add those extra ' simple quotes around XPath expressions, they become simple strings.
So you should remove the simple quotes and obtain something like:

Code: Select all

<xsl:if test="//ab:kov-data/ab:person1/ab:gender = //ab:kov-data/ab:person2/ab:gender">
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
poroton
Posts: 4
Joined: Thu Aug 09, 2018 3:29 pm

Re: check values of 2 XPath-Expressions

Post by poroton »

This solves my question. Thank you for helping me. :)
Post Reply