XML string that is equal to one of the attributes

I want to check if there is a string that is equal to one of the attributes. For instances:

<rules> <mother cat="pp"> <daughter cat="pr"/> <daughter cat="np"/> </mother> <mother cat="wp"> <daughter cat="rp"/> <daughter cat="vp"/> </mother> <mother cat="cn"> <daughter cat="jj"/> <daughter cat="cn"/> </mother> <mother cat="np"> <daughter cat="jj"/> <daughter cat="np"/> </mother> </rules> 

I just need to check if "pp" matches one of the cat attributes "mothers". For this example, yes. But for "pr" this should be wrong.

Thanks.

+4
source share
4 answers

Use the following expression:

 /*/mother[@cat='pp'] 

This expression returns a mother element that has an attribute named cat whose value is pp .

In the xsl:if test expression, this will return true if such a node exists; false otherwise. For instance:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:if test="/*/mother[@cat='pp']">NODE EXISTS</xsl:if> </xsl:template> </xsl:stylesheet> 

This style sheet prints NODE EXISTS only when a node is present in the source.

Find this item position using the following expression:

 count(/*/mother[@cat='pp']/preceding-sibling::*)+1 
+3
source

This XPath syntax will work for your case ...

 //mother[@cat='pp'] 

It will return any mother nodes that have the value "pp" in the cat attribute. Otherwise, if you tested

 //mother[@cat='pr'] 

Then you will get an empty node.

+1
source

It:

 <xsl:if test="//mother[@cat = 'pp']"> <xsl:message terminate="no"> Exists! </xsl:message> </xsl:if> 

Exists! will be printed Exists! If there is at least one mother with @cat = 'pp' .

+1
source

I just need to check if "pp" matches one of the mother’s cats attribute.

Good question, +1.

Easier than all the other answers :

 /*/mother/@cat = 'pp' 

This evaluates to true() exactly when "pp" is equal to the string value of the cat attribute of the mother element, which is a child of the top element of the XML document.

In this example, yes. But for "pr" this should be wrong.

Evaluate again :

 /*/mother/@cat = 'pr' 

The following is a simple conversion showing this in action :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> /*/mother/@cat = 'pp' is <xsl:text/> <xsl:value-of select="/*/mother/@cat = 'pp'"/> /*/mother/@cat = 'pr' is <xsl:text/> <xsl:value-of select="/*/mother/@cat = 'pr'"/> </xsl:template> </xsl:stylesheet> 

when applied to the provided XML document :

 <rules> <mother cat="pp"> <daughter cat="pr"/> <daughter cat="np"/> </mother> <mother cat="wp"> <daughter cat="rp"/> <daughter cat="vp"/> </mother> <mother cat="cn"> <daughter cat="jj"/> <daughter cat="cn"/> </mother> <mother cat="np"> <daughter cat="jj"/> <daughter cat="np"/> </mother> </rules> 

required, the correct result is obtained :

  /*/mother/@cat = 'pp' is true /*/mother/@cat = 'pr' is false 
0
source

Source: https://habr.com/ru/post/1384782/


All Articles