Java 尝试使用 Eclipse 为 xml 运行 xslt,但它不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24125596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to run an xslt for xml with eclipse but it does not work
提问by Saraki
I am trying to run a .xslt to show some info from a .xml to an html page in eclipse. I run it, and eclipse produces a .out.xml file which when I try to run with chrome I get the message :
我正在尝试运行 .xslt 以在 Eclipse 中显示从 .xml 到 html 页面的一些信息。我运行它,eclipse 生成一个 .out.xml 文件,当我尝试使用 chrome 运行时,我收到消息:
This XML file does not appear to have any style information associated with it.
此 XML 文件似乎没有任何与之关联的样式信息。
I am completely new in this so I cannot figure out what is going on...
我对此完全陌生,所以我无法弄清楚发生了什么......
XML FILE :
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 FILE:
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:
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="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>
采纳答案by davidfmatheson
Eclipse seems to like its style sheets named with .xsl
instead of .xslt
. At least, when I tried your style sheet and input XML in my environment, I renamed it as such and changing the second line of your XML to
Eclipse 似乎喜欢它的样式表,.xsl
而不是.xslt
. 至少,当我尝试您的样式表并在我的环境中输入 XML 时,我将其重命名并将 XML 的第二行更改为
<?xml-stylesheet type="text/xsl" href="html_xml.xsl"?>
and changing the second line of your XSL to
并将 XSL 的第二行更改为
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
as suggested in another comment. After that, the following worked for me:
正如另一条评论中所建议的那样。之后,以下对我有用:
- File named
input.xml
andhtml_xml.xsl
in the same directory in an Eclipse project - Right-click on
input.xml
and selectRun As
->XSL Transformation
- Look for file named
input.out.html
generated
- 在 Eclipse 项目中命名
input.xml
并html_xml.xsl
位于同一目录中的文件 - 右键单击
input.xml
并选择Run As
->XSL Transformation
- 查找名为
input.out.html
生成的文件
回答by Isaac G Sivaa
Please save as Out.htmlfile your output in eclipse. If you open html file with xml extension , we will get error as
请将您在 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>
回答by Deepu Surendran
- Select XSLT file Run As -- Runconfiguration
- Create New configuration under XSL
- Select input file(test.xml) and transformation file (test.xslt)
- Select classpath tab and under user entries add external jar (Saxon-HE-9.5.0.1.jar)
- Select Output file from ouput tab
- 选择 XSLT 文件 Run As -- Runconfiguration
- 在 XSL 下创建新配置
- 选择输入文件(test.xml)和转换文件(test.xslt)
- 选择类路径选项卡并在用户条目下添加外部 jar (Saxon-HE-9.5.0.1.jar)
- 从输出选项卡中选择输出文件
回答by rahul
Select xml file and click on run as xsl transformation. then select file with .xsl and click ok. After that one file generated something.out.xml then change the extension of something.out.xml to something.out.html and run tht html file
选择 xml 文件并单击作为 xsl 转换运行。然后选择带有 .xsl 的文件并单击确定。在该文件生成 something.out.xml 之后,将 something.out.xml 的扩展名更改为 something.out.html 并运行该 html 文件