当前位置: 代码迷 >> XML/SOAP >> <xsl:for-each select= 取嵌套节点则么写,该如何解决
  详细解决方案

<xsl:for-each select= 取嵌套节点则么写,该如何解决

热度:1723   发布时间:2014-03-01 00:37:46.0
<xsl:for-each select= 取嵌套节点则么写
<root>
  <techs>
    <tech>
       <title>aaa</title>
       <groups>
          <group>
             <name>group1</name>
          </group>
          <group>
             <name>group2</name>
          </group>
       </groups>
    </tech> 
    <tech>
       <title>bbb</title>
       <groups>
          <group>
             <name>group1</name>
          </group>
       </groups>
    </tech>
  </techs>
</root>

现在我想把每个tech节点的内容都列出来,包括title和groups里的每个group的name
怎么取到group name的值?
<xsl:template match="/">
<xsl:for-each select="root/techs/tech">
   <xsl:value-of select="title" />  // 这个title可以取到值
   <xsl:for-each select="groups/group"> // select表达式该如何写???
       <xsl:value-of select="name" />  // 这里name取不到
   </xsl:for-each>
</xsl:for-each>
</xsl:template>不清楚你的为什么取不到,我的可以取到:
xml:
<?xml-stylesheet type="text/xsl" href="transfer_test.xslt" ?>
<root>
  <techs>
    <tech>
       <title>aaa</title>
       <groups>
          <group>
             <name>group1</name>
          </group>
          <group>
             <name>group2</name>
          </group>
       </groups>
    </tech> 
    <tech>
       <title>bbb</title>
       <groups>
          <group>
             <name>group1</name>
          </group>
       </groups>
    </tech>
  </techs>
</root>


xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

  <xsl:template match="/">
    <xsl:for-each select="root/techs/tech">
      <span>
        <xsl:value-of select="title"/>
      </span>
      <xsl:for-each select="groups/group">
        <br />
        <span>
          <xsl:value-of select="name"/>
        </span>
      </xsl:for-each>
      <br />
      <br />
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>


结果:
  相关解决方案