给定这个xml
<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是什么?
<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中完成循环。
谢谢,阿尔
发布于 2009-04-17 15:25:45
<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>
发布于 2009-04-17 16:14:44
实际上,有一个比Tomalak提出的解决方案稍微简单一些的解决方案:
<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文档上应用此转换时,将生成所需的结果
<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>
注意到
<xsl:apply-templates>
指令没有显式指定必须处理的节点列表。在这里,我们依赖于这样一个事实,即文档中唯一的非空白节点是我们具有匹配模板的节点的子节点。对于这个特定的XML文档,我们甚至可以将<xsl:template match="SectionName | FileName">
<xsl:template match="text()">
的匹配模式更改为: result.<xsl:strip-space>
directive.,而转换仍然会产生所希望的用法。
发布于 2009-04-17 15:24:18
像这样的东西应该能起到作用:
<?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
https://stackoverflow.com/questions/760763
复制相似问题