以下是xsl文档的摘录(技术上在模板内):
<table>
<tr>
<th>INSTANCE</th>
<th>SWVER</th>
<th>SYSTEMID</th>
<th>SYSTIME</th>
<th>SYSMEM</th>
<th>CUTMEM</th>
<th>FILEMEM</th>
<th>CALCONFIG</th>
</tr>
<tr>
<td><xsl:value-of select='@INSTANCE'/></td>
<td><xsl:value-of select='@SWVER'/></td>
<td><xsl:value-of select='@SYSTEMID'/></td>
<td><xsl:value-of select='@SYSTIME'/></td>
<td><xsl:value-of select='@SYSMEM'/></td>
<td><xsl:value-of select='@CUTMEM'/></td>
<td><xsl:value-of select='@FILEMEM'/></td>
<td><xsl:value-of select="@CALCONFIG"/></td>
</tr>
</table>
是否有某种方法可以避免将表格标题和属性选择都写出属性的冗余?我无法使用外部资源。
我在想我可以定义一些包含基本结构的xsl变量,如下所示并从那里生成表。
<list>
<item>INSTANCE</item>
...
<item>CALCONFIG</item>
</list>
XML数据只是一堆标签,具有相同的值,至少包含上面列出的属性。每个标签看起来都像这样:
<THING INSTANCE="boop" SWVER="foo" SYSTEMID="123"
...
CALCONFIG="cal.cfg" SOMETHINGELSE="bar"
/>
答案 0 :(得分:2)
为了说明我在对您的问题的评论中提出的观点,请考虑以下示例:
<强> XML 强>
<root>
<item color="red" name="alpha" size="small" garbage="123" id="1"/>
<item color="green" name="bravo" size="medium" garbage="456" id="2"/>
<item color="blue" name="charlie" size="large" garbage="789" id="3"/>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<xsl:output method="html" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<my:columns>
<col>id</col>
<col>name</col>
<col>size</col>
<col>color</col>
</my:columns>
<xsl:variable name="columns" select="document('')/xsl:stylesheet/my:columns" />
<xsl:template match="/root">
<table border="1">
<tr>
<xsl:for-each select="$columns/col">
<th>
<xsl:value-of select="." />
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="item">
<xsl:variable name="attributes" select="@*" />
<tr>
<xsl:for-each select="$columns/col">
<td>
<xsl:value-of select="$attributes[name()=current()]" />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
结果(已呈现)
答案 1 :(得分:-1)
Bryant,你需要使用两个文件(XML和XSL):
首先,您需要指定一个模板(example.xsl):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>instance</th>
<th>swver</th>
<th>systemid</th>
<th>systime</th>
<th>sysmem</th>
<th>cutmem</th>
<th>filemem</th>
<th>calconfig</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="instance"/></td>
<td><xsl:value-of select="swver"/></td>
<td><xsl:value-of select="systemid"/></td>
<td><xsl:value-of select="systime"/></td>
<td><xsl:value-of select="sysmem"/></td>
<td><xsl:value-of select="cutmem"/></td>
<td><xsl:value-of select="filemem"/></td>
<td><xsl:value-of select="calconfig"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
然后你需要根据模板(example.xsl)定义你的XML文件(example.xml):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<catalog>
<cd>
<instance>instance1</instance>
<swver>swver1</swver>
<systemid>systemid1</systemid>
<sysmem>sysmem1</sysmem>
<systime>systime1</systime>
<cutmem>cutmem1</cutmem>
<filemem>filemem1</filemem>
<calconfig>calconfig1</calconfig>
</cd>
<cd>
<instance>instance2</instance>
<swver>swver2</swver>
<systemid>systemid2</systemid>
<sysmem>sysmem2</sysmem>
<systime>systime2</systime>
<cutmem>cutmem2</cutmem>
<filemem>filemem2</filemem>
<calconfig>calconfig2</calconfig>
</cd>
.
.
</catalog>
<强>结果:强>
希望这有帮助!