我正在尝试一次性使用XML,XSL和CSS进行学校作业,我无法在网上找到一个好的答案。我已将我的xml文件链接到我的CSS文件,它似乎没有读取它。我在XSL文件中尝试过它也没有任何积极的结果。我只是尝试将CSS类和属性应用于我的XML文件。提前感谢您的帮助,我已经尝试了几个小时,但找不到我的错误。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<?xml-stylesheet type="text/css" href="cdList.css"?>
<catalog>
<cd>
<artist>The Briefs</artist>
<country>USA</country>
<albums>
<album>
<name>Hit After Hit</name>
<year>2002</year>
<songs>
<song>Poor And Weird</song>
<song>I'm A Raccoon</song>
<song>Sylvia</song>
<song>Where Did He Go?</song>
</songs>
</album>
<album>
<name>Off The Charts</name>
<year>2004</year>
<songs>
<song>Ain't It The Truth</song>
<song>Tear It In Two</song>
<song>We Americans</song>
<song>Ouch Ouch Ouch</song>
</songs>
</album>
</albums>
</cd>
<cd>
<artist>Dethklok</artist>
<country>USA</country>
<albums>
<album>
<name>The Deth Album</name>
<year>2007</year>
<songs>
<song>Go Into The Water</song>
<song>Awaken</song>
<song>Bloodrocuted</song>
<song>Go Forth And Die</song>
<song>Fansong</song>
<song>Better Metal Snake</song>
<song>The Lost Vikings</song>
<song>Thunderhorse</song>
<song>Birthday Dethday</song>
<song>Hatredcopter</song>
<song>Castratikron</song>
<song>Face Fisted</song>
<song>Dethharmonic</song>
</songs>
</album>
<album>
<name>Dethalbum II</name>
<year>2009</year>
<songs>
<song>Bloodlines</song>
<song>The Gears</song>
<song>Burn The Earth</song>
<song>Laser Cannon Deth Sentence</song>
<song>Black Fire Upon Us</song>
<song>Dethsupport</song>
<song>The Cyborg Slayers</song>
<song>I Tamper With Evidence</song>
<song>Murmaider II: The Water God</song>
<song>Comet Song</song>
<song>Symmetry</song>
<song>Volcano</song>
</songs>
</album>
<album>
<name>Dethalbum III</name>
<year>2011</year>
<songs>
<song>Crush The Industry</song>
<song>Andromeda</song>
<song>The Galaxy</song>
<song>Starved</song>
<song>Killstardo Abominate</song>
<song>Ghostqueen</song>
<song>Biological Warfare</song>
<song>Skyhunter</song>
<song>The Hammer</song>
<song>Rejoin</song>
</songs>
</album>
</albums>
</cd>
</catalog>
<?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>Britts Cd Collection</h2>
<table border="1">
<tr>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Country</th>
<th style="text-align:left">Year</th>
<th style="text-align:left">Album name</th>
<th style="text-align:left">Song List</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="country"/></td>
<xsl:for-each select="albums/album">
<tr>
<td colspan="2"></td>
<td><xsl:value-of select="year" /></td>
<td><xsl:value-of select="name" /></td>
<xsl:for-each select="songs/song">
<tr>
<td colspan="4"></td>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:for-each>
</tr>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
cd {
display: block;
}
artist {
background-color: yellow;
border: 2px solid green;
display: block;
margin-bottom: 10px;
}
country {
background-color: yellow;
border: 2px solid green;
display: block;
margin-bottom: 10px;
}
albums {
display: block;
}
album {
display: block;
}
name {
display: block;
}
year {
display: block;
}
songs {
display: block;
}
song {
display: block;
}
.test {
background-color: yellow;
border: 2px solid green;
display: block;
margin-bottom: 10px;
}
答案 0 :(得分:0)
将CSS应用于XM有两种方法。不幸的是,你正试图将两者混合在一起,这是行不通的。
您的CSS样式表显然旨在将直接应用于您的XML:它包含cd
,artist
,country
等的选择器 - 这些是XML中的所有元素。这些元素在XSL转换的结果中不,它将XML转换为包含(主要)表的HTML文档。要为表格设置样式,您需要选择表格的元素,例如tr
和td
。
如果删除了XSLT样式表的链接,只保留CSS样式表的链接,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="cdList.css"?>
<catalog>
<cd>
...
然后您可以直接在浏览器中显示XML文件,并通过链接的CSS设置样式。
如果,OTOH,您想先将XML转换为HTML,那么您需要:
只保留另一个链接,即XSLT样式表。
接下来,您需要修改XSLT样式表以便生成结果 HTML包含指向CSS样式表的链接。您可以通过插入:
来完成此操作 <head>
<link rel="stylesheet" type="text/css" href="mynewcss.css"/>
</head>
进入<html>
标记,位于<body>
元素之前。
最后,您需要编写mynewcss.css
样式表,并使其样式化XSLT转换的结果中的元素,而不是源XML。