首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >xslt:如何创建动态表?

xslt:如何创建动态表?
EN

Stack Overflow用户
提问于 2009-04-17 15:02:49
回答 3查看 1.3K关注 0票数 1

给定这个xml

代码语言:javascript
运行
复制
<Documents>
<Section>
<SectionName>Green</SectionName>
  <Document>
    <FileName>Tier 1 Schedules</FileName>     
  </Document>
  <Document>
    <FileName>Tier 3 Schedules</FileName>      
  </Document>
  <Document>
    <FileName>Setback Schedule</FileName>    
  </Document>
  <Document>
    <FileName>Tier 2 Governance</FileName>    
  </Document>
</Section>
<Section>
<SectionName>MRO/Refurb</SectionName>
  <Document>
    <FileName>Tier 2 Governance</FileName>    
  </Document>
</Section>
</Documents>

输出此html的xslt是什么?

代码语言:javascript
运行
复制
<table>
  <tr>
    <td>Green</td>
  </tr>
  <tr>
    <td>Tier 1 Schedules</td>
  </tr>
  <tr>
    <td>Tier 3 Schedules</td>
  </tr>
  <tr>
    <td>Setback Schedule</td>
  </tr>
  <tr>
    <td>Tier 2 Governance</td>
  </tr>
  <tr>
    <td>MRO/Refurb</td>
  </tr>
  <tr>
    <td>Tier 2 Governance</td>
  </tr>
</table>

我可以在asp.net中做到这一点,但不确定如何在xslt中完成循环。

谢谢,阿尔

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-04-17 15:25:45

代码语言:javascript
运行
复制
<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

  <xsl:template match="/">
    <table>
      <xsl:apply-templates select="//SectionName | //FileName" />
    </table>
  </xsl:template>

  <xsl:template match="SectionName | FileName">
    <tr>
      <td><xsl:value-of select="." /></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
票数 3
EN

Stack Overflow用户

发布于 2009-04-17 16:14:44

实际上,有一个比Tomalak提出的解决方案稍微简单一些的解决方案:

代码语言:javascript
运行
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:strip-space elements="*"/>

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

    <xsl:template match="SectionName | FileName">
        <tr>
            <td>
                <xsl:value-of select="." />
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

在最初提供的XML文档上应用此转换时,将生成所需的结果

代码语言:javascript
运行
复制
<table>
   <tr>
      <td>Green</td>
   </tr>
   <tr>
      <td>Tier 1 Schedules</td>
   </tr>
   <tr>
      <td>Tier 3 Schedules</td>
   </tr>
   <tr>
      <td>Setback Schedule</td>
   </tr>
   <tr>
      <td>Tier 2 Governance</td>
   </tr>
   <tr>
      <td>MRO/Refurb</td>
   </tr>
   <tr>
      <td>Tier 2 Governance</td>
   </tr>
</table>

注意到

  1. The <xsl:apply-templates> 指令没有显式指定必须处理的节点列表。在这里,我们依赖于这样一个事实,即文档中唯一的非空白节点是我们具有匹配模板的节点的子节点。对于这个特定的XML文档,我们甚至可以将<xsl:template match="SectionName | FileName"> <xsl:template match="text()">的匹配模式更改为: result.
  2. The <xsl:strip-space> directive.

,而转换仍然会产生所希望的用法。

票数 2
EN

Stack Overflow用户

发布于 2009-04-17 15:24:18

像这样的东西应该能起到作用:

代码语言:javascript
运行
复制
<?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:output method="html" indent="yes"/>

    <xsl:template match="/">
      <table>
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
      </table>
    </xsl:template>

  <xsl:template match="SectionName">
    <tr>
      <td>
        <xsl:value-of select="."/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="Document">
    <tr>
      <td>
        <xsl:value-of select="FileName"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Marc

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/760763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档