当前位置:首页 > 技术问答集
firefox

初学xml的小题,,答对就有分哟!!!!

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

///////////////  
  <?xml   version="1.0"?>  
  <?xml-stylesheet   type="text/xsl"   href="test2.xsl"?>  
  <test>  
          <x   a="1">  
              <x   a="2"   b="b">  
                  <x>  
                      <y>y31</y>  
                      <y>y32</y>  
                  </x>  
              </x>  
          </x>  
  </test>  
  /////////////////////  
  test2.xsl  
  <?xml   version=1.0?>  
  <xsl:stylesheet   version="1.0"  
              xmlns:xsl="http://www.w3.org/1999/xsl/transform">  
        <xsl:output   method="xml"   omit-xml-declaration="yes"   indent="yes"/>  
   
        <!--   suppress   text   nodes   not   covered   in   subsequent   template   rule.   -->  
        <xsl:template   match="text()"/>  
   
      <!--   handles   a   generic   element   node.   -->  
        <xsl:template   match="*">  
              <xsl:element   name="{name()}">  
                    <xsl:apply-templates   select="*|@*"   />  
                    <xsl:if   test="text()">  
                          <xsl:value-of   select="."/>  
                    </xsl:if>  
              </xsl:element>  
        </xsl:template>  
   
        <!--   handles   a   generic   attribute   node.   -->  
        <xsl:template   match="@*">  
              <xsl:attribute   name="{name()}">  
                    <xsl:value-of   select="."/>  
              </xsl:attribute>  
        </xsl:template>  
   
         
  </xsl:stylesheet>  
  结果为:y31   y32    
  去掉<!--   handles   a   generic   attribute   node.   -->  
        <xsl:template   match="@*">  
              <xsl:attribute   name="{name()}">  
                    <xsl:value-of   select="."/>  
              </xsl:attribute>  
        </xsl:template>  
  时结果为:12by31   y32       有谁能解释上面这一段的起什么作用吗,?/  
  2:  
      |     为     union的意思   ,相当于和的意思了,对不对,与其它语言的意思不同了?  
  eg:    
      expression:     first-name   |   last-name          
      refers   to     :       a   node   set   containing   <first-name>   and   <last-name>    
   
  elements   in   the   current   context.  
     
   
    ////////////////  
  3::::  
  <?xml   version="1.0"?>  
  <?xml-stylesheet   type="text/xsl"   href="test2.xsl"?>  
  <test>  
          <x   a="1">  
              <x   a="2">  
                  <x>  
                      <y>y31</y>  
                      <y>y32</y>  
                  </x>  
              </x>  
          </x>  
          <x   a="1">  
              <x   a="2">  
                  <y>y21</y>  
                  <y>y22</y>  
              </x>  
          </x>  
          <x   a="1">  
              <y>y11</y>  
              <y>y12</y>  
          </x>  
          <x>  
              <y>y03</y>  
              <y>y04</y>  
          </x>  
  </test>  
   
   
  <?xml   version=1.0?>  
  <xsl:stylesheet   version="1.0"  
              xmlns:xsl="http://www.w3.org/1999/xsl/transform">  
   
  <xsl:output   method="text"   />  
   
  <xsl:template   match="/">  
                      <xsl:value-of   select="count(//x)"/>  
                      <xsl:value-of   select="count(//x[1])"/>  
                        <xsl:value-of   select="count(//x/y)"/>  
                  <xsl:value-of   select="count(//x/y[1])"/>  
            <xsl:value-of   select="count(//x[1]/y[1])"/>  
  </xsl:template>  
   
  </xsl:stylesheet>  
  谁能解释一下这为什么是74842  
   
 

· 网友精彩回答:

发表者:cds27

首先,你要先了解一下那段xsl的功能,它其实只是将xml再复制一遍。  
        <xsl:template   match="@*">  
              <xsl:attribute   name="{name()}">  
                    <xsl:value-of   select="."/>  
              </xsl:attribute>  
        </xsl:template>  
  该模板匹配所有属性节点,功能是添加该属性。换句话说,就是复制属性。也就是  
  <x   a="1">  
  的  
  a="1"  
   
      |     为     union的意思   ?   对。  
  上例中的意思为,当前节点下的所有的子元素节点和属性节点。  
   
  count是计数的意思,count(//x)就是统计所有的x个数。  
  count(//x[1])是所有x位置为1的节点个数。  
  count(//x/y)是所有x下的y的个数。  
  其他类似。详细参考xpath。

.
© 2006-2008 All Rights Reserved