0

I have a soap message

 <soapenv:Envelope 
     xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Body>
         <id>0</id>
         <id>1</id>
       </soapenv:Body>
  </soapenv:Envelope>

I want to loop through the id elements and check if the current one has a value = 1 but my xslt is not working.

   <xsl:template match="/">
         <xsl:for-each select="node()">
          <xsl:if test="current()/text='1'">
          do something
          </xsl:if>
         </xsl:for-each>
   </xsl:template> 

Can someone point out what I do wrong and give me directions on how to proceed?

EDIT: I need something that will return true when one of id's is equal to 1, otherwise false .

0

2 Answers 2

1

I want to loop through the id elements and check if the current one has a value = 1

You don't need to do that. The following expression:

/soapenv:Envelope/soapenv:Body/id=1

will return true if one or more of the id elements has the value of 1, false otherwise.


Demo: https://xsltfiddle.liberty-development.net/ej9EGc8

Sign up to request clarification or add additional context in comments.

3 Comments

@banb It's not a workaround. That's how comparisons work in XPath (as well as most languages that deal with sets).
wrong use of words but thank you, I will read upon that. Sorry to ask but can you show me a way to return true if all the id's are equal to 1?
I think I have just answered this here: stackoverflow.com/a/55157336/3016153
1
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="soapenv:Envelope">
        <xsl:for-each select="soapenv:Body">
            <xsl:if test="id='1'">
                do something
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
You may use like this

1 Comment

Thank you @imran. That was very helpful but can you see my post's edit and help me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.