在浏览器中查看xsl的输出

mdh*_*deh 1 xml xslt

我有一个xml文件.我想通过xsl.so更改某些样式的xml元素.我也有一个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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

MiM*_*iMo 5

放置test.xmltest.xsl放在同一目录中然后加载test.xml到浏览器中 - ?xml-stylesheet开头的指令将导致xsl被浏览器加载和执行.

话虽如此,您的XSL应用于您的XML测试文件会产生以下输出:

<Text Font="Arial" Bold="true" Color="Red"></Text>
<Text Font="Sans" Italic="true"></Text>
Run Code Online (Sandbox Code Playgroud)

这是无效的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>
Run Code Online (Sandbox Code Playgroud)

生成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>
Run Code Online (Sandbox Code Playgroud)