2017-04-15 3 views
0

J'ai des problèmes. Tout d'abord je m'excuse si les mots que j'utilise ne sont pas les mots corrects, je suis assez nouveau pour XML.Utiliser des sous-éléments dans un fichier XSLT?

Dans mon devoir, on m'a demandé de créer un fichier DTD, XML et XSLT. J'ai terminé le fichier DTD et XML, et je travaille sur le fichier XSLT. Tout fonctionne, je ne sais pas comment accéder aux éléments qui sont à l'intérieur d'autres éléments. Voici le code, ce sera plus facile à expliquer.

XML; (Seulement une partie de celui-ci pour un exemple)

<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> 

Tout cela crée quelque chose qui ressemble à ceci;

enter image description here

Comme vous pouvez le voir, tout élément XML dans un autre élément ne sera pas affiché à l'intérieur de la table. Je voudrais n'importe quelle aide sur comment je pourrais résoudre ceci. Je vous remercie.

Répondre

1

Vous pouvez spécifier le chemin d'accès à l'élément dans l'attribut select.

au lieu de le faire ....

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

Vous devez procéder, comme RAM est sous l'élément Specs

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

De même, pour obtenir le moniteur, par exemple, faire

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