如何在XML

时间:2016-03-26 00:22:07

标签: xml xslt

我在XML上添加了属性,我需要编辑XSL文件才能显示汽车属性,如何编辑才能在XML上看到它。 这是我的XML:

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

谢谢你提前我尝试了不同的方法,但我不能让它发挥作用。

2 个答案:

答案 0 :(得分:1)

您要找的是:<xsl:value-of select="accessories/@radio" />

答案 1 :(得分: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>
相关问题