2013-08-18 21 views
0

我有一个xml文件。我想通过xsl.so更改xml元素的一些样式我也有一个xsl文件。然后我想查看浏览器中的更改,但我不知道我怎样才能做到这一点?在浏览器中查看xsl的输出

xml文件:(的test.xml)

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="test2.xsl"?> 


<root> 
    <Text Style='style1'></Text> 
    <Text Style='style2'></Text> 
</root> 

XSL文件:(test.xsl)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:output method="html"/> 
    <xsl:attribute-set name="style1"> 
     <xsl:attribute name="Font">Arial</xsl:attribute> 
     <xsl:attribute name="Bold">true</xsl:attribute> 
     <xsl:attribute name="Color">Red</xsl:attribute> 
    </xsl:attribute-set> 
    <xsl:attribute-set name="style2"> 
     <xsl:attribute name="Font">Sans</xsl:attribute> 
     <xsl:attribute name="Italic">true</xsl:attribute> 
    </xsl:attribute-set> 
    <xsl:template match="Text[@Style='style1']"> 
     <xsl:copy use-attribute-sets="style1"> 
      <xsl:copy-of select="@*[name()!='Style']"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Text[@Style='style2']"> 
     <xsl:copy use-attribute-sets="style2"> 
      <xsl:copy-of select="@*[name()!='Style']"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

回答

4

广场test.xml并在同一目录test.xsl然后加载test.xml在浏览器中 - 开头的?xml-stylesheet指令将导致浏览器加载和执行xsl。

说了这么多,你的XSL应用到你的XML测试文件产生这样的输出:

<Text Font="Arial" Bold="true" Color="Red"></Text> 
<Text Font="Sans" Italic="true"></Text> 

无效HTML,所以你不会看到在浏览器中任何东西。

要看到一些输出尝试使用此XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:output method="html"/> 

    <xsl:template match="Text[@Style='style1']"> 
    <p style="font-family: Arial; font-weight: bold; color: red"> 
     <xsl:apply-templates/> 
    </p> 
    </xsl:template> 

    <xsl:template match="Text[@Style='style2']"> 
    <p style="font-family: Sans-Serif; font-style: italic"> 
     <xsl:apply-templates/> 
    </p> 
    </xsl:template> 

</xsl:stylesheet> 

与style属性产生p HTML标签。还需要注意的是,你需要一些文本应用样式在你的XML测试文件添加到:

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="test2.xsl"?> 
<root> 
    <Text Style='style1'>Text in style 1</Text> 
    <Text Style='style2'>Text in style 2</Text> 
</root> 
+0

没有任何办法看到在浏览器中输出我想这个属性,看STYLE1(宋体,?大胆,红色)。 –

+0

@mdharaghizadeh:查看更新的答案 – MiMo