首页 > 解决方案 > 使用 xslt 循环遍历简单的 xml

问题描述

我有一条肥皂消息

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

我想遍历 id 元素并检查当前元素的值是否 = 1 但我的 xslt 不起作用。

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

有人可以指出我做错了什么并指导我如何进行吗?

编辑:我需要一些在 id 之一等于 1 时返回 true 的东西,否则返回 false 。

标签: xmlxsltxslt-2.0

解决方案


<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

推荐阅读