在XSLT文件中使用子元素?

时间:2017-04-15 15:01:23

标签: xml xslt dtd

我遇到了麻烦。首先,如果我使用的单词不是正确的单词,我很抱歉,我对XML很陌生。

在我的任务中,我被要求创建一个DTD,XML和XSLT文件。我完成了DTD和XML文件,我正在处理XSLT文件。一切正常,我只是不知道如何访问其他元素中的元素。这是代码,因此它更容易解释。

XML; (仅作为示例的一部分)

<entry id='c01'>
    <MetaTags>Business</MetaTags>   
    <title><brand>HP Pavilion</brand><name>550-112NA</name></title>
    <Description>While other towers have been standing still, HP has revolutionized the category. From magnified performance and reliability, to its stylish redesign, this HP Pavilion is the best thing to happen to towers in over 20 years.</Description>
    <Price>€579</Price>
    <Image src="Image1.jpg"/>
    <Specs>
        <CPU>A10-8750 APU</CPU>
        <GPU>Radeon R7</GPU>
        <RAM>8 GB DDR3</RAM>
        <Storage><HDD> 2TB </HDD></Storage>
        <OS>Windows 10</OS>
        <optional>
            <Monitor>LG 22" Full HD TV</Monitor>
            <Keyboard>Microsoft Wired Keyboard 600</Keyboard>
            <Mouse>Logitech M705 Mouse</Mouse>              
        </optional>
    </Specs>
</entry>

XSLT;

 <?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>
<head>
    <title>Computer Shop</title>
</head>

<body>
<table border="1">
<tr bgcolor="#9acd32">

<th style="text-align:left">Image</th>
<th style="text-align:left">MetaTags</th>
<th style="text-align:left">Brand</th>
<th style="text-align:left">Name</th>
<th style="text-align:left">Description</th>
<th style="text-align:left">Price</th>
<th style="text-align:left">CPU</th>
<th style="text-align:left">GPU</th>
<th style="text-align:left">RAM</th>
<th style="text-align:left">HDD</th>
<th style="text-align:left">SSD</th>
<th style="text-align:left">OS</th>
<th style="text-align:left">Monitor</th>
<th style="text-align:left">Keyboard</th>
<th style="text-align:left">Mouse</th>
</tr>
<xsl:for-each select="ComputerShop/entry">
<tr>

<td>
<xsl:value-of select="Image"/>
</td>

<td>
<xsl:value-of select="MetaTags"/>
</td>

<td>
<xsl:value-of select="brand"/>
</td>

<td>
<xsl:value-of select="name"/>
</td>

<td>
<xsl:value-of select="Description"/>
</td>

<td>
<xsl:value-of select="Price"/>
</td>

<td>
<xsl:value-of select="RAM"/>
</td>

<td>
<xsl:value-of select="RAM"/>
</td>

<td>
<xsl:value-of select="HDD"/>
</td>

<td>
<xsl:value-of select="SSD"/>
</td>

<td>
<xsl:value-of select="OS"/>
</td>

<td>
<xsl:value-of select="Monitor"/>
</td>

<td>
<xsl:value-of select="Keyboard"/>
</td>

<td>
<xsl:value-of select="Mouse"/>
</td>





</tr>
</xsl:for-each>
</table>
</body>





</html>
</xsl:template>
</xsl:stylesheet>

这一切都创造了这样的东西;

enter image description here

如您所见,其他元素中的任何XML元素都不会显示在表格内。我想知道如何解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在select属性中指定元素的路径。

而不是这样做....

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

您应该这样做,因为RAM位于Specs元素

 <xsl:value-of select="Specs/RAM"/>

同样,要获取监视器,例如,请执行此操作

 <xsl:value-of select="Specs/optional/Monitor"/>