bfury
bfury

Reputation: 99

Loop through simple xml using xslt

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 .

Upvotes: 0

Views: 87

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117100

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

Upvotes: 1

imran
imran

Reputation: 461

<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

Upvotes: 1

Related Questions