尝试使用eclipse运行xslt for xml,但它不起作用

时间:2014-06-09 17:30:36

标签: java xml eclipse xslt

我正在尝试运行.xslt来显示从.xml到eclipse中的html页面的一些信息。我运行它,eclipse生成一个.out.xml文件,当我尝试使用chrome运行时,我收到消息:

  

此XML文件似乎没有任何关联的样式信息   用它。

我是全新的,所以我无法弄清楚发生了什么......

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="html_xml.xslt"?>
<CarModel>
<Audi model = "TT" year = "2006" starting_price = "33.000$">
    <type>sport</type>
    <horse_power>222hp</horse_power>
    <drivetrain>quattro</drivetrain>
    <transmission>6_Manual</transmission>
</Audi>
<Mercedes model = "W222_S400" year = "2013" starting_price = "63.000$">
    <type>luxury</type>
    <horse_power>302hp</horse_power>
    <drivetrain>front_wheel_drive</drivetrain>
    <transmission>7_Automatic</transmission>
</Mercedes>
<BMW model = "X3_xDrive35i" year = "2010" staring_price = "40.000$">
    <type>crossover_SUV</type>
    <horse_power>302hp</horse_power>
    <drivetrain>quattro</drivetrain>
    <transmission>6_Manual</transmission>
</BMW>
</CarModel>

XSLT文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
  <html><body><h2> Car Model Info </h2>
  <table border="1">
    <xsl:apply-templates mode="elem" select="/*"/>
  </table></body>
  </html>
</xsl:template>
<xsl:template mode="elem" match="/*">
  <tr bgcolor="#9acd32">
    <td><xsl:value-of select="name()"/></td>
    <td><xsl:apply-templates mode="child" select="/*/*"/> (c) </td>
    <td><xsl:apply-templates mode="attr" select="/*/@*"/> (a) </td>
    </tr>
</xsl:template>
<xsl:template mode="child" match="/*/*">
  <xsl:value-of select="name()"/>
</xsl:template>
<xsl:template mode="attr" match="/*/@*">
  <xsl:value-of select="name()"/>
</xsl:template>

</xsl:stylesheet>

XSLT OUTPUT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
  <html><body><h2> Car Model Info </h2>
  <table border="1">
    <xsl:apply-templates mode="elem" select="/*"/>
  </table></body>
  </html>
</xsl:template>
<xsl:template mode="elem" match="a:*">
  <tr bgcolor="#9acd32">
    <td><xsl:value-of select="name()"/></td>
    <td><xsl:apply-templates mode="child" select="/*/*"/> (c) </td>
    <td><xsl:apply-templates mode="attr" select="/*/@*"/> (a) </td>
    </tr>
</xsl:template>
<xsl:template mode="child" match="/*/*">
  <xsl:value-of select="name()"/>
</xsl:template>
<xsl:template mode="child" match="/*/@*">
  <xsl:value-of select="name()"/>
</xsl:template>

</xsl:stylesheet>

4 个答案:

答案 0 :(得分:2)

Eclipse似乎喜欢用.xsl而不是.xslt命名的样式表。至少,当我在我的环境中尝试样式表并输入XML时,我将其重命名为并将XML的第二行更改为

<?xml-stylesheet type="text/xsl" href="html_xml.xsl"?>

并将XSL的第二行更改为

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

正如另一条评论中所建议的那样。之后,以下内容对我有用:

  1. 在Eclipse项目的同一目录中名为input.xmlhtml_xml.xsl的文件
  2. 右键点击input.xml并选择Run As - &gt; XSL Transformation
  3. 查找名为input.out.html已生成的文件

答案 1 :(得分:0)

请在eclipse中保存为 Out.html 文件输出。 如果你用xml扩展名打开html文件,我们将收到错误

 This XML file does not appear to have any style information associated with it. The document tree is shown below.

<html>
  <body>
    <h2>Car Model Info</h2>
      <table border="1">
       <tr bgcolor="#9acd32">
         <td>CarModel</td>
         <td>AudiMercedesBMW (c)</td>
         <td>(a)</td>
       </tr>
    </table>
  </body>
</html>

答案 2 :(得分:0)

  1. 选择XSLT文件“运行方式 - 运行配置”
  2. 在XSL下创建新配置
  3. 选择输入文件(test.xml)和转换文件(test.xslt)
  4. 选择classpath选项卡,在用户条目下添加外部jar(Saxon-HE-9.5.0.1.jar)
  5. 从输出标签
  6. 中选择输出文件

答案 3 :(得分:0)

选择xml文件,然后单击“以xsl转换运行”。      然后 使用.xsl选择文件,然后单击“确定”。 之后生成一个文件     然后是something.out.xml 改变扩展名     something.out.xml来     something.out.html 并运行html文件