4

I want to check if there is a string which 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 test if "pp" is equal to one of the mothers' cat attribute. For this example yes it is. But it should be false for "pr".

Thank You.

Ataman
  • 2,530
  • 3
  • 22
  • 34

4 Answers4

3

Use the following expression:

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

This expression returns the mother element having an attribute named cat whose value is equal to pp.

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

<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 stylesheet prints NODE EXISTS only when such a node is present in the source.

Find this element's position using the following expression:

count(/*/mother[@cat='pp']/preceding-sibling::*)+1
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • and how can i find the position of that mother for example for pp it should be 1. i tried count preceding-sibling but didnt work out.. – Ataman Dec 05 '11 at 21:25
  • @user446141 - Something like this: `count(/*/mother[@cat='pp']/preceding-sibling::*)+1.`. See: http://stackoverflow.com/questions/226405/find-position-of-a-node-using-xpath – Wayne Dec 05 '11 at 21:30
1

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 were testing

//mother[@cat='pr']

Then you would get an empty node set back.

Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
1

This:

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

Will print Exists! If there is at least one mother with @cat = 'pp'.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
0

I just need to test if "pp" is equal to one of the mothers' cat attribute.

Good question, +1.

Simpler than all other present answers:

/*/mother/@cat = 'pp'

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

For this example yes it is. But it should be false for "pr".

Once again, evaluate:

/*/mother/@cat = 'pr'

Below is a simple transformation 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 on 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>

the wanted, correct result is produced:

  /*/mother/@cat = 'pp' is true

  /*/mother/@cat = 'pr' is false
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431