2017-04-03 34 views
0

我想要改造這個類型的XML調色板文件:使用此XSL轉換變換調色板文件的HTML內容使用XSLT

<?xml version="1.0"?> 
<?xml-stylesheet href="Palette.xsl" type="text/xsl"?> 
<Palette Name="MyPalette"> 
    <PaletteEntry Color="#D0427B" Color2="#D0427B" /> 
    <PaletteEntry Color="#D55588" Color2="#D55588" /> 
    <PaletteEntry Color="#DA6895" Color2="#DA6895" /> 
</Palette> 

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

<xsl:template match="Palette"> 
<html> 
    <head> 
     <title>Palette {@Name}</title> 
     <!--link rel="stylesheet" href="Palette.css" type="text/css"/--> 
    </head> 
    <body> 
     <div class="palette"> 
      <xsl:apply-templates/> 
     </div> 
    </body> 
</html> 
</xsl:template> 

<xsl:template match="PaletteEntry"> 
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: {@Color}; border: 2px solid {@Color2};"> 
     <xsl:apply-templates select="@*"/> 
    </div> 
</xsl:template> 

<xsl:template match="@Color"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

<xsl:template match="@Color2"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

</xsl:stylesheet> 

在輸出的問題是,顏色屬性在屬性中時呈現良好,但未在內容中翻譯。下面是HTML輸出:

<html> 
 

 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
    <title>Palette {@Name}</title> 
 
</head> 
 

 
<body> 
 
    <div class="palette"> 
 
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;" color="495359" color2="#495359"></div> 
 
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;" color="5B646A" color2="#5B646A"></div> 
 
    </div> 
 
</body> 
 

 
</html>

我怎樣才能顏色值複製到內容?

預期輸出:

<html> 
 

 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
 
    <title>Palette "MyPalette"</title> 
 
</head> 
 

 
<body> 
 
    <div class="palette"> 
 
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #495359; border: 2px solid #495359;">#495359<br/>#495359</div> 
 
    <div class="palette-entry" style="width:40px;height:40px; margin:2px; float:left; background-color: #5B646A; border: 2px solid #5B646A;">#5B646A<br/>#5B646A</div> 
 
    </div> 
 
</body> 
 

 
</html>

+1

可以告訴你,你居然想到在這種情況下,輸出?謝謝 –

回答

1

ColorColor2是在XML屬性,因此使用xsl:copy將創建結果樹的屬性(雖然在Color的情況下,你是先使用xsl:attribute創建一個新屬性)。

你其實只是要輸出的屬性值,所以使用xsl:value-of代替

<xsl:template match="@Color"> 
    <xsl:value-of select="." /> 
    <br /> 
</xsl:template> 

<xsl:template match="@Color2"> 
    <xsl:value-of select="." /> 
</xsl:template>