将XML文件转换为HTML页面

时间:2017-05-22 21:12:27

标签: html xml xslt

我尝试将XML文件转换为HTML页面但不显示任何内容。在下面,我把XML代码,xslt代码和HTML页面。谢谢你的帮助。

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="fg.xsl" ?>

<cuimodel>
<window id='window1'>
<div>
<label id='label1'> searchPreference </label> 
<label id='label12'> seeFirst </label> 
<combobox id='CB1' name='criteriaCB'>
<item>promotions</item>
<item>BestRated </item>
</combobox>
</div>

<div>
<label id='label2'> mon </label> 
<label id='label21'> second  </label> 
<combobox id='CB2' name='DispLayout'>
<item>GridLayout</item>
<item>listlayout</item>
</combobox> 
</div>

<div>
<label id='label13'> Preference </label> 
<label id='label122'> see </label> 
<combobox id='CB3' name='CT'>
<item>HighContrast</item>
<item>LowContrast</item>
</combobox>
</div>
<button>cancel</button>
<button>ok</button>
</window>
</cuimodel>

XSLT代码

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> 
<html> 
<body> 
<xsl:for-each select="div">
<xsl:for-each select="label">
<label>
<xsl:value-of select="."/>
</label>
<select>
<xsl:for-each select="combobox">
<option>
<xsl:value-of select="item"/>
</option>
</xsl:for-each>
</select>
</xsl:for-each>
<xsl:for-each select="button">
<button>
<xsl:value-of select="."/>
</button>
</xsl:for-each> 
</xsl:for-each>
</body> 
</html>
</xsl:template>
</xsl:stylesheet>

html页面

enter image description here

1 个答案:

答案 0 :(得分:1)

您有几个问题,从:

开始
<xsl:for-each select="div">

这不会选择任何内容,因为您位于/根节点的上下文中。要从此处选择div,您需要:

<xsl:for-each select="cuimodel/window/div">

另请注意:

<xsl:value-of select="item"/>

不返回任何内容,因为item元素没有文本值。也许你的意思是:

<xsl:value-of select="item/@value"/>

但这也有问题,因为combobox有多个item个孩子。

可能还有更多,但这应该让你开始。