2016-03-26 7 views
1

自分のXMLに属性を追加しました。車の属性を表示できるようにXSLファイルを編集する必要があります。XMLで表示するにはどうすれば編集できますか? これは私のXMLです:XMLにXSL属性を表示する方法

<?xml version = "1.0" encoding = "utf-8" ?> 
 
<?xml-stylesheet type = "text/xsl" href = "7.5.xsl" ?> 
 
<CarCatalog> 
 
    <CarItem> 
 
     <make>Nissan</make> 
 
     <model>Altima</model> 
 
     <year>2016</year> 
 
     <color>Red</color> 
 
     <engine> 
 
      <number_of_cylinders>6</number_of_cylinders> 
 
      <fuel_system>fuel</fuel_system> 
 
     </engine> 
 
     <number_of_doors>4</number_of_doors> 
 
     <transmission_type>Auto</transmission_type> 
 
     <accessories radio="Yes" air_conditioning="Yes" power_windows="Yes" Power_steering="Yes" Power_brakes="No" /> 
 
    </CarItem> 
 
</CarCatalog>

これは、XSLの一部です:

<?xml version="1.0" encoding="utf-8" ?> 
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> 
 

 
    <xsl:template match="CarCatalog"> 
 

 
     <xsl:for-each select="CarItem"> 
 
      <span style="font-style: italic; color: blue;"> Year: </span> 
 
      <xsl:value-of select="year" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Make: </span> 
 
      <xsl:value-of select="make" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Model: </span> 
 
      <xsl:value-of select="model" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Color: </span> 
 
      <xsl:value-of select="color" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Engine: </span> 
 
      <xsl:value-of select="engine" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Number of doors: </span> 
 
      <xsl:value-of select="number_of_doors" /> 
 
      <br /> 
 
      <span style="font-style: italic; color: blue;"> Transmission type: </span> 
 
      <xsl:value-of select="transmission_type" /> 
 
      <br /> 
 
    <!-- Here I got stock -->   
 
      <span style="font-style: italic; color: blue;"> Radio: </span> 
 
      <xsl:attribute name="accessories"> 
 
       <xsl:value-of select="." /> 
 
      </xsl:attribute> 
 

 

 
      <br /> 
 
      <br /> 
 
     </xsl:for-each> 
 
    </xsl:template> 
 
</xsl:stylesheet>

は、あなたが、私はさまざまな方法を試しましたが、私はできません進めるありがとうそれを機能させる。あなたが探しているもの

答えて

1

は次のとおりです。ところで<xsl:value-of select="accessories/@radio" />

0

、あなたのスパン与えられた同一であり、結果のテキストは、元のノード名と一致し、<xsl:for-loop>せずにXSLTを一般化する余地がある。

<?xml version="1.0" encoding="utf-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/> 

    <xsl:template match="CarCatalog" priority="2"> 
    <html> 
     <body> 
      <div> 
       <xsl:apply-templates select="*"/> 
      </div> 
     </body> 
    </html> 
    </xsl:template>  

    <xsl:template match="CarItem" priority="3"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="CarItem/*[node()]|accessories/@*" priority="4">   
      <span style="font-style: italic; color: blue;"> 
       <xsl:value-of select="concat(translate(name(.),'_',' '), ': ')"/> 
      </span> 
      <xsl:value-of select="." />      
      <br /> 
    </xsl:template>  

    <xsl:template match="accessories" priority="5"> 
     <xsl:apply-templates select="@*"/> 
    </xsl:template> 

</xsl:stylesheet> 

出力

<html> 
    <body> 
     <div> 
     <span style="font-style: italic; color: blue;">make: </span>Nissan<br/> 
     <span style="font-style: italic; color: blue;">model: </span>Altima<br/> 
     <span style="font-style: italic; color: blue;">year: </span>2016<br/> 
     <span style="font-style: italic; color: blue;">color: </span>Red<br/> 
     <span style="font-style: italic; color: blue;">engine: </span> 
      6 
      fuel 
     <br/> 
     <span style="font-style: italic; color: blue;">number of doors: </span>4<br/> 
     <span style="font-style: italic; color: blue;">transmission type: </span>Auto<br/> 
     <span style="font-style: italic; color: blue;">radio: </span>Yes<br/> 
     <span style="font-style: italic; color: blue;">air conditioning: </span>Yes<br/> 
     <span style="font-style: italic; color: blue;">power windows: </span>Yes<br/> 
     <span style="font-style: italic; color: blue;">Power steering: </span>Yes<br/> 
     <span style="font-style: italic; color: blue;">Power brakes: </span>No<br/> 
     </div> 
    </body> 
</html> 
関連する問題