hi saucer.我最近在开发中遇到一个问题,就是不知道怎么消除重复的XML标记,
for example:
<?xml version="1.0" encoding="utf-16"?><man><name>Raymond </name>Raymond <hair x="0" y="0" layer="6 "><style id="1" name="1.png " path="\content\wardrobe\Library\Raymond\hair\1.png" /></hair></man><man><name>Raymond </name><hair x="0" y="0" layer="6 "><style id="2" name="10.png " path="\content\wardrobe\Library\Raymond\hair\10.png" /></hair></man>template.xsl file
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:template match="/NewDataSet/Table">
    <man>    
        <name>
       <xsl:value-of select="rolename"/>        
      </name>
      <xsl:apply-templates select="rolename"/>
      <hair>
          <xsl:attribute name="x">
                    <xsl:value-of select="hairx"/>            
          </xsl:attribute>      
          <xsl:attribute name="y">
                    <xsl:value-of select="hairy"/>
          </xsl:attribute>
          <xsl:attribute name="layer">
                    <xsl:value-of select="layer"/>
          </xsl:attribute>      
          <style>
          <xsl:attribute name="id">
                    <xsl:value-of select="hairid"/>
          </xsl:attribute>
          <xsl:attribute name="name">
            <xsl:value-of select="hairname"/>
          </xsl:attribute>
        <xsl:attribute name="path">
          <xsl:value-of select="hairpath"/>
        </xsl:attribute>
      </style>      
    </hair>
    </man>
  </xsl:template>
</xsl:stylesheet>我想要的的xml 标记是:<man>
<name>Raymond</name>
<hair x="0" y="0" layer="0">
<style id="0" name="xxxx" path="xxx.png">
<style id="0" name="xxxx" path="xxx.png">
</man>thanks so much!

解决方案 »

  1.   

    最后得到的格式是:
    <man>
       <name>Raymond</name>
       <body>
         <style id="1" name="white" path="image/Raymond/body/white.png" />
         <style id="1" name="black" path="image/Raymond/body/black.png" />     
       </body>
       <hair x="0" y="0" layer="1">
         <style id="1" name="样式一" path="image/Raymond/hair/1.png" />
         <style id="1" name="样式二" path="image/Raymond/hair/2.png" />
         <style id="1" name="样式三" path="image/Raymond/hair/3.png" />
         <style id="1" name="样式四" path="image/Raymond/hair/4.png" />
         <style id="1" name="样式五" path="image/Raymond/hair/5.png" />
         <style id="1" name="样式六" path="image/Raymond/hair/6.png" />
         <style id="1" name="样式七" path="image/Raymond/hair/7.png" />
         <style id="1" name="样式八" path="image/Raymond/hair/8.png" />
         <style id="1" name="样式九" path="image/Raymond/hair/9.png" />
         <style id="1" name="样式十" path="image/Raymond/hair/10.png" />
         <style id="1" name="样式十一" path="image/Raymond/hair/11.png" />
       </hair>
       <shirt x="0" y="0" layer="2">
         <style id="1" name="样式一" path="image/Raymond/shirt/1.png" />
         <style id="1" name="样式二" path="image/Raymond/shirt/2.png" />
         <style id="1" name="样式三" path="image/Raymond/shirt/3.png" />
         <style id="1" name="样式四" path="image/Raymond/shirt/4.png" />
         <style id="1" name="样式五" path="image/Raymond/shirt/5.png" />
         <style id="1" name="样式六" path="image/Raymond/shirt/6.png" />
       </shirt>
       <pants x="0" y="0" layer="3">
         <style id="1" name="样式一" path="image/Raymond/pants/1.png" />
         <style id="1" name="样式二" path="image/Raymond/pants/2.png" />
         <style id="1" name="样式三" path="image/Raymond/pants/3.png" />
         <style id="1" name="样式四" path="image/Raymond/pants/4.png" />
         <style id="1" name="样式五" path="image/Raymond/pants/5.png" />
         <style id="1" name="样式六" path="image/Raymond/pants/6.png" />
         <style id="1" name="样式七" path="image/Raymond/pants/7.png" />
         <style id="1" name="样式八" path="image/Raymond/pants/8.png" />
       </pants>
       <overcoat x="0" y="0" layer="4">
         <style id="1" name="样式一" path="image/Raymond/overcoat/1.png" />
         <style id="1" name="样式二" path="image/Raymond/overcoat/2.png" />
       </overcoat>
       <shoes x="0" y="0" layer="5">
         <style id="1" name="样式一" path="image/Raymond/shoes/1.png" />
         <style id="1" name="样式二" path="image/Raymond/shoes/2.png" />
         <style id="1" name="样式三" path="image/Raymond/shoes/3.png" />
         <style id="1" name="样式四" path="image/Raymond/shoes/4.png" />
         <style id="1" name="样式五" path="image/Raymond/shoes/5.png" />
         <style id="1" name="样式六" path="image/Raymond/shoes/6.png" />
       </shoes>
    </man>
      

  2.   

    可参考下面的例子
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="a.xsl"?>
    <books>
      <book>
        <title>孟子E章</title>
      </book>
      <book>
        <title>孟子E章2</title>
      </book>
      <book>
        <title>孟子E章</title>
      </book>
      <book>
        <title>孟子E章2</title>
      </book>
      <book>
        <title>孟子E章</title>
      </book>
    </books><?xml version="1.0" ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:key name="kbook" match="book" use="title"/>
      <xsl:template match="/">
        <table>
          <xsl:apply-templates select="node()|@*"/>
        </table>
      </xsl:template>
      <xsl:template match="book">
        <xsl:if test="generate-id()= generate-id(key('kbook',title)[1])">
          <tr>
            <td>
              <xsl:value-of select="title"/>
            </td>
            <td>
              <xsl:value-of select="count(key('kbook',title))"/>
            </td>
          </tr>
        </xsl:if>
      </xsl:template>
      <xsl:template match="text()"/>
    </xsl:stylesheet>
      

  3.   

    thanks so [email protected]