您的位置:程序门 -> .net技术 -> c#



关于xslt的for-each用法


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于xslt的for-each用法[已结贴,结贴人:ismezy2002]
发表于:2007-03-23 17:06:58 楼主
原格式如下:
<span   row= "1 "   col= "1 "/>
<span   row= "1 "   col= "2 "/>  
<span   row= "2 "   col= "1 "/>
.....................

目标格式:
<row   index= "1 ">
    <col   index= "1 "/>
</row>
.........

应该如何处理?
发表于:2007-03-23 17:30:511楼 得分:30
没看出什么对应关系。可以使用xslt将一个xml转换成另外一个xml的,
类似
http://blog.csdn.net/net_lover/archive/2002/05/16/6900.aspx
发表于:2007-03-23 17:37:442楼 得分:0
目标格式:
<row   index= "1 ">
    <col   index= "1 "/>
    <col   index= "2 "/>
</row>
<row   index= "2 ">
    <col   index= "1 ">
    <col   index= "2 ">
</row>
........
这样没有对应吗?
发表于:2007-03-26 09:27:133楼 得分:0
不懂,帮顶
发表于:2007-03-26 09:31:354楼 得分:0
what?
发表于:2007-03-26 09:42:155楼 得分:20
cdcatalog.html

<html>
<body> <script   type= "text/javascript "> //   load   xml  
var   xml   =   new   activexobject( "microsoft.xmldom ")
xml.async   =   false
xml.load( "cdcatalog.xml ")//   load   xsl
var   xsl   =   new   activexobject( "microsoft.xmldom ")
xsl.async   =   false
xsl.load( "cdcatalog.xsl ")//   transform
document.write(xml.transformnode(xsl)) </script> </body>
</html>


------------------------
cdcatalog.xml

<?xml   version= "1.0 "   encoding= "iso-8859-1 "?>
<catalog>
    <cd>
        <title> empire   burlesque </title>
        <artist> bob   dylan </artist>
        <country> usa </country>
        <company> columbia </company>
        <price> 10.90 </price>
        <year> 1985 </year>
    </cd>
</catalog>


------------------------
cdcatalog.xsl


<?xml   version= "1.0 "   encoding= "iso-8859-1 "?>
<xsl:stylesheet   version= "1.0 "
xmlns:xsl= "http://www.w3.org/1999/xsl/transform "> <xsl:template   match= "/ ">
    <html>
    <body>
        <h2> my   cd   collection </h2>  
        <table   border= "1 ">
            <tr   bgcolor= "#9acd32 ">
                <th   align= "left "> title </th>  
                <th   align= "left "> artist </th>  
            </tr>
            <xsl:for-each   select= "catalog/cd ">                     ----   for-each读取xml节点
            <tr>
                <td> <xsl:value-of   select= "title "   /> </td>
                <td> <xsl:value-of   select= "artist "   /> </td>
            </tr>
            </xsl:for-each>
    </table>
    </body>
    </html>
</xsl:template> </xsl:stylesheet>
发表于:2007-03-26 09:44:026楼 得分:20
xsl:foreach----------循环span
xsl:if   (@row)
creamteelement(row)
xsl:if   (@col)
createelement(col)
.............


伪代码,
下面有结构转换的例子
http://blog.csdn.net/honkerhero/archive/2007/03/14/1528837.aspx
发表于:2007-03-27 09:02:597楼 得分:0
解决了,用分组的方法实现
    <xsl:key   name= "row "   match= "@row "   use= ". "/>
      <xsl:for-each   select= "span/@row[generate-id()=generate-id(key( 'row ',.))] ">
                <row   index= "{current()+1} ">
                          <xsl:for-each   select= "../../span[@row   =   current()] ">
                                    <cell   index= "{@col} ">
                                              <xsl:value-of   select= "@text "/>
                                      </cell>
                                </xsl:for-each>
                  </row>
        </xsl:for-each>
发表于:2007-03-27 09:03:528楼 得分:30
http://blog.csdn.net/net_lover/archive/2002/05/16/6900.aspx
发表于:2007-03-27 09:14:009楼 得分:0
<?xml   version= "1.0 "   encoding= "utf-8 "?>
<!--   edited   with   xml   spy   v4.4   u   (http://www.xmlspy.com)   by   opec   (opec)   -->
<xsl:stylesheet   version= "1.0 "   xmlns:xsl= "http://www.w3.org/1999/xsl/transform ">
  <xsl:template   match= "/ ">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template   match= "table ">
    <table>
      <xsl:if   test= "@id!= ' ' ">
        <xsl:attribute   name= "id "> <xsl:value-of   select= "@id "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@cssclass!= ' ' ">
        <xsl:attribute   name= "class "> <xsl:value-of   select= "@cssclass "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@style!= ' ' ">
        <xsl:attribute   name= "style "> <xsl:value-of   select= "@style "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@halign!= ' ' ">
        <xsl:attribute   name= "align "> <xsl:value-of   select= "@halign "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@valign!= ' ' ">
        <xsl:attribute   name= "valign "> <xsl:value-of   select= "@valign "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@width!= ' ' ">
        <xsl:attribute   name= "width "> <xsl:value-of   select= "@width "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@height!= ' ' ">
        <xsl:attribute   name= "height "> <xsl:value-of   select= "@height "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@bgcolor!= ' ' ">
        <xsl:attribute   name= "bgcolor "> <xsl:value-of   select= "@bgcolor "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@border!= ' ' ">
        <xsl:attribute   name= "border "> <xsl:value-of   select= "@border "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@bordercolor!= ' ' ">
        <xsl:attribute   name= "bordercolor "> <xsl:value-of   select= "@bordercolor "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@cellpadding!= ' ' ">
        <xsl:attribute   name= "cellpadding "> <xsl:value-of   select= "@cellpadding "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@cellspacing!= ' ' ">
        <xsl:attribute   name= "cellspacing "> <xsl:value-of   select= "@cellspacing "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@onclick!= ' ' ">
        <xsl:attribute   name= "onclick "> <xsl:value-of   select= "@onclick "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@ondbclick!= ' ' ">
        <xsl:attribute   name= "ondbclick "> <xsl:value-of   select= "@ondbclick "/> </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </table>
  </xsl:template>
  <xsl:template   match= "datarow ">
    <tr>
      <xsl:if   test= "@cssclass!= ' ' ">
        <xsl:attribute   name= "class "> <xsl:value-of   select= "@cssclass "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@style!= ' ' ">
        <xsl:attribute   name= "style "> <xsl:value-of   select= "@style "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@halign!= ' ' ">
        <xsl:attribute   name= "align "> <xsl:value-of   select= "@halign "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@valign!= ' ' ">
        <xsl:attribute   name= "valign "> <xsl:value-of   select= "@valign "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@width!= ' ' ">
        <xsl:attribute   name= "width "> <xsl:value-of   select= "@width "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@height!= ' ' ">
        <xsl:attribute   name= "height "> <xsl:value-of   select= "@height "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@bgcolor!= ' ' ">
        <xsl:attribute   name= "bgcolor "> <xsl:value-of   select= "@bgcolor "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@bordercolor!= ' ' ">
        <xsl:attribute   name= "bordercolor "> <xsl:value-of   select= "@bordercolor "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@onclick!= ' ' ">
        <xsl:attribute   name= "onclick "> <xsl:value-of   select= "@onclick "/> </xsl:attribute>
      </xsl:if>
      <xsl:if   test= "@ondbclick!= ' ' ">
        <xsl:attribute   name= "ondbclick "> <xsl:value-of   select= "@ondbclick "/> </xsl:attribute>
      </xsl:if>
      <xsl:for-each   select= "column[@isdisplay= 'true '] ">
        <td>
          <xsl:if   test= "@cssclass!= ' ' ">
            <xsl:attribute   name= "class "> <xsl:value-of   select= "@cssclass "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@style!= ' ' ">
            <xsl:attribute   name= "style "> <xsl:value-of   select= "@style "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@colspan!= ' ' ">
            <xsl:attribute   name= "colspan "> <xsl:value-of   select= "@colspan "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@rowspan!= ' ' ">
            <xsl:attribute   name= "rowspan "> <xsl:value-of   select= "@rowspan "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@halign!= ' ' ">
            <xsl:attribute   name= "align "> <xsl:value-of   select= "@halign "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@valign!= ' ' ">
            <xsl:attribute   name= "valign "> <xsl:value-of   select= "@valign "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@width!= ' ' ">
            <xsl:attribute   name= "width "> <xsl:value-of   select= "@width "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@height!= ' ' ">
            <xsl:attribute   name= "height "> <xsl:value-of   select= "@height "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@bgcolor!= ' ' ">
            <xsl:attribute   name= "bgcolor "> <xsl:value-of   select= "@bgcolor "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@bordercolor!= ' ' ">
            <xsl:attribute   name= "bordercolor "> <xsl:value-of   select= "@bordercolor "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@onclick!= ' ' ">
            <xsl:attribute   name= "onclick "> <xsl:value-of   select= "@onclick "/> </xsl:attribute>
          </xsl:if>
          <xsl:if   test= "@ondbclick!= ' ' ">
            <xsl:attribute   name= "ondbclick "> <xsl:value-of   select= "@ondbclick "/> </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates/>
        </td>
      </xsl:for-each>
    </tr>
  </xsl:template>
  <xsl:template   match= "value ">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template   match= "br ">
    <br/>
    <xsl:value-of   select= ". "/>
  </xsl:template>
    <xsl:template   match= "page ">
    <p   style= "page-break-after:always "> </p>
  </xsl:template>
</xsl:stylesheet>
发表于:2007-03-27 12:35:4010楼 得分:0
楼上照办我blog上的东西,不厚道啊


快速检索

最新资讯
热门点击