English 中文(简体)
XSL - loop through sub nodes in XML
原标题:XSL - loop through sub nodes in XML
  • 时间:2010-01-27 10:16:25
  •  标签:
  • html
  • xml
  • xslt

我很想通过XML档案来证明:

<node>
  <cd name="td1">
    <data value="cd1-0" />
    <cd name="td2">
      <data value="cd1-1" />
    </cd>
    <cd name="td3">
      <data value="cd1-2" />
    </cd>
  </cd>
  <cd name="td4">
    <data value="cd2-0" />
  </cd>
</node>

这是结果。

<html>  
  <table border="1">  
    <tr>  
      <td>cd1-0</td>  
      <td></td>  
    </tr>  
    <tr>  
      <td></td>  
      <td>cd1-1</td>  
    </tr>  
    <tr>  
      <td></td>  
      <td>cd1-2</td>  
    </tr>  
    <tr>  
      <td>cd2-0</td>  
      <td></td>  
    </tr>  
  </table>  
</html>

在这一考试中,I级有2级:tdecd。 但这一水平可能有限。 因此,需要某种补假功能。

问题回答

这将与任何层次的定型编号cd要素有关。

您必须修改一个比照,以制作<代码><html>(and <head/><...</one>架构,可在match= node 模板上查阅。

它将跳出无需开车的空拖车<td/>

<>XSL

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:template match="node">
       <xsl:element name="table">
        <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="cd">   
        <xsl:element name="tr">
            <xsl:for-each select="ancestor::cd">
                <xsl:element name="td"/>
            </xsl:for-each>
            <xsl:element name="td">
                <xsl:value-of select="./data/@value" />
            </xsl:element>
        </xsl:element>
        <xsl:apply-templates />
    </xsl:template>

</xsl:stylesheet>

<><>Output

<table>
  <tr><td>cd1-0</td></tr>        <!-- Here the second <td/> is skipped -->
  <tr><td/><td>cd1-1</td></tr>
  <tr><td/><td>cd1-2</td></tr>
  <tr><td>cd2-0</td></tr>        <!-- Here the second <td/> is skipped -->
</table>

这样做的一种方式:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <!-- adapt output method and doctype to your needs -->
  <xsl:output 
    method="html" 
    doctype-system="http://www.w3.org/TR/html4/strict.dtd" 
    doctype-public="-//W3C//DTD HTML 4.01//EN" 
    indent="yes" 
  /> 

  <!-- the document root becomes html -->
  <xsl:template match="/">
    <html>
      <xsl:apply-templates select="*" />
    </html>
  </xsl:template>

  <!-- node becomes table -->
  <xsl:template match="node">
    <table border="1">
      <xsl:apply-templates select="*" />
    </table>
  </xsl:template>

  <!-- 1st level cd elements (children of node) go into first td -->
  <xsl:template match="node/cd">
    <tr>
      <td><xsl:value-of select="data/@value" /></td>
      <td />
    </tr>
  </xsl:template>

  <!-- 2nd level cd elements (children of cd) go into second td -->
  <xsl:template match="cd/cd">
    <tr>
      <td />
      <td><xsl:value-of select="data/@value" /></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

你发出的XML并不像一个完整的XML。 各位可以向您介绍,在Xslt使用每个元素。

指称:

<table>
<xsl:for-each select="node/cd">
<tr>
<td>
<xsl:value-of select="data/@value"/>
</td>
</tr>
</xsl:for-each>
</table>

http://www.w3schools.com/Xsl/xsl_for_each.asp”rel=“nofollow noreferer” http://www.w3schools.com/Xsl/xsl_for_each.asp

希望你们会这样做。

Update: Thanks to subtenante for clearing up the xml. You can use template to solve this

<xsl:template match="node">
 <html>
  <body>
   <table border="1">
      <xsl:apply-templates select="cd" />
   </table>
  </body>
 </html>
</xsl:template>

<xsl:template match="cd">
 <tr>
  <td>
   <xsl:value-of select="@name" />
  </td>
  <td>
   <xsl:value-of select="data/@value"/>
  </td>
 </tr> 

 <xsl:if test="cd">
  <xsl:apply-templates select="cd" />
 </xsl:if>
</xsl:template>

表格如下:

<table border="1">
<tbody>
<tr>
 <td>td1</td>
 <td>cd1-0</td>
</tr>
<tr>
 <td>td2</td>
 <td>cd1-1</td>
</tr>
<tr>
 <td>td3</td>
 <td>cd1-2</td>
</tr>
<tr>
 <td>td4</td>
 <td>cd2-0</td>
</tr>
</tbody></table>

提供的斜线需要修改,以达到你所希望的目标。 如果是的话,请予以罚款。 页: 1

资料来源:

Ramjee





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签