我坚持使用XSLT将XML转换为HTML,其中XML中的记录包含多个具有相同名称但不同标签,标签和内容的嵌套元素。
我的XML包含以下记录:
<catalog>
<catalogKey>77971</catalogKey>
<yearOfPublication>1999</yearOfPublication>
<marc>
<marcEntry tag="035" label="Local system #" ind=" ">77971</marcEntry>
<marcEntry tag="035" label="Local system #" ind=" ">DIT87496</marcEntry>
<marcEntry tag="245" label="Title" ind=" 0">3D Studio MAX 3 fundamentals [electronic resource].</marcEntry>
<marcEntry tag="260" label="Publication info" ind=" ">Indianapolis, Ind. : New Riders, 1999.</marcEntry>
<marcEntry tag="300" label="Physical descript" ind=" ">1 computer laser optical disk</marcEntry>
<marcEntry tag="500" label="General Note" ind=" ">Use with book: 3D Studio MAX 3 fundamentals / Michael Todd Peterson</marcEntry>
<marcEntry tag="520" label="Abstract/summary" ind=" ">"The CD-ROM is loaded with .MAX and .AVI files to follow along with the tutorials, including plug-ins, images, sample animations, models, and textures. Bonus Lotus ScreenCam movies are included to enhance the learning process by providing a visual reference for each tutorial."</marcEntry>
<marcEntry tag="538" label="Technical details" ind=" ">CD-ROM disk</marcEntry>
<marcEntry tag="650" label="Subject term" ind=" 0">Computer graphics--Computer programs.</marcEntry>
<marcEntry tag="650" label="Subject term" ind=" 0">3d studio (Computer program)</marcEntry>
</marc>
<call>
<item>
<copyNumber>1</copyNumber>
<itemID>DUT2000-2102</itemID>
<library>STEVEBIKO</library>
<libraryDescription>Alan Pittendrigh Library (Steve Biko Campus)</libraryDescription>
<location>DISCARD</location>
<homeLocation>DISCARD13</homeLocation>
<price currency="R" >80.25</price>
<category1>CDROMS</category1>
<type>MMSTLN</type>
<numberOfPieces>1</numberOfPieces>
<dateCreated>2005-11-11</dateCreated>
<isPermanent>true</isPermanent>
</item>
</call>
</catalog>
我需要在HTML中显示:
77971 1999 035 77971
035 DIT87496
245 3D Studio MAX 3 fundamentals
...
1 DUT2000-2102 STEVEBIKO DISCARD DISCARD13 80.25 CDROMS MMSTLN 1 2005-11-11
但我能够取得的最好成就是:
77971 1999 77971
1 DUT2000-2102 STEVEBIKO DISCARD DISCARD13 80.25 CDROMS MMSTLN 1 2005-11-11
使用:
<?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>
<xsl:for-each select="report/catalog">
<tr>
<td><xsl:value-of select="catalogKey"/></td>
<td><xsl:value-of select="yearOfPublication"/></td>
<xsl:for-each select="marc">
<td>
<xsl:value-of select="marcEntry"/>
</td>
</xsl:for-each>
</tr>
<xsl:for-each select="call">
<xsl:for-each select="item">
<tr>
<td></td>
<td></td>
<td></td>
<td><xsl:value-of select="copyNumber"/></td>
<td><xsl:value-of select="itemID"/></td>
<td><xsl:value-of select="library"/></td>
<td><xsl:value-of select="location"/></td>
<td><xsl:value-of select="homeLocation"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="category1"/></td>
<td><xsl:value-of select="type"/></td>
<td><xsl:value-of select="numberOfPieces"/></td>
<td><xsl:value-of select="dateCreated"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我需要帮助的是将具有相同名称但不同标签和内容的多个嵌套元素转换为HTML。 marcEntry标签会因记录而异。
答案 0 :(得分:0)
看起来你正在寻找这样的事情:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="report/catalog">
<xsl:variable name="currentCatalog" select="." />
<xsl:variable name="marcEntries" select="marc/marcEntry" />
<xsl:for-each select="$marcEntries">
<tr>
<td>
<xsl:if test="position() = 1">
<xsl:value-of select="$currentCatalog/catalogKey"/>
</xsl:if>
</td>
<td>
<xsl:if test="position() = 1">
<xsl:value-of select="$currentCatalog/yearOfPublication"/>
</xsl:if>
</td>
<td>
<xsl:value-of select="@tag" />
</td>
<td colspan="10">
<xsl:value-of select="." />
</td>
</tr>
</xsl:for-each>
<xsl:for-each select="call">
<xsl:for-each select="item">
<tr>
<td/>
<td/>
<td/>
<td>
<xsl:value-of select="copyNumber"/>
</td>
<td>
<xsl:value-of select="itemID"/>
</td>
<td>
<xsl:value-of select="library"/>
</td>
<td>
<xsl:value-of select="location"/>
</td>
<td>
<xsl:value-of select="homeLocation"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="category1"/>
</td>
<td>
<xsl:value-of select="type"/>
</td>
<td>
<xsl:value-of select="numberOfPieces"/>
</td>
<td>
<xsl:value-of select="dateCreated"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果对您的示例输入运行此操作(在使用<report>
标记包装后),则输出如下(单击运行代码段以查看其呈现):
<html>
<body>
<table>
<tr>
<td>77971</td>
<td>1999</td>
<td>035</td>
<td colspan="10">77971</td>
</tr>
<tr>
<td></td>
<td></td>
<td>035</td>
<td colspan="10">DIT87496</td>
</tr>
<tr>
<td></td>
<td></td>
<td>245</td>
<td colspan="10">3D Studio MAX 3 fundamentals [electronic resource].</td>
</tr>
<tr>
<td></td>
<td></td>
<td>260</td>
<td colspan="10">Indianapolis, Ind. : New Riders, 1999.</td>
</tr>
<tr>
<td></td>
<td></td>
<td>300</td>
<td colspan="10">1 computer laser optical disk</td>
</tr>
<tr>
<td></td>
<td></td>
<td>500</td>
<td colspan="10">Use with book: 3D Studio MAX 3 fundamentals / Michael Todd Peterson</td>
</tr>
<tr>
<td></td>
<td></td>
<td>520</td>
<td colspan="10">"The CD-ROM is loaded with .MAX and .AVI files to follow along with the tutorials, including plug-ins, images, sample animations, models, and textures. Bonus Lotus ScreenCam movies are included to enhance the learning process by providing a visual reference for each tutorial."</td>
</tr>
<tr>
<td></td>
<td></td>
<td>538</td>
<td colspan="10">CD-ROM disk</td>
</tr>
<tr>
<td></td>
<td></td>
<td>650</td>
<td colspan="10">Computer graphics--Computer programs.</td>
</tr>
<tr>
<td></td>
<td></td>
<td>650</td>
<td colspan="10">3d studio (Computer program)</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>1</td>
<td>DUT2000-2102</td>
<td>STEVEBIKO</td>
<td>DISCARD</td>
<td>DISCARD13</td>
<td>80.25</td>
<td>CDROMS</td>
<td>MMSTLN</td>
<td>1</td>
<td>2005-11-11</td>
</tr>
</table>
</body>
</html>
&#13;