通过DOMSource的JAXP Transformer返回样式表 [英] JAXP Transformer via DOMSource returns stylesheet

查看:74
本文介绍了通过DOMSource的JAXP Transformer返回样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,似乎找不到任何解决方案.我只是试图将XSLT样式表应用于XML文件(在这种情况下,SPARQL查询结果的格式设置为XML,但是任何其他XML文件都提供相同的结果).在这种情况下,我需要通过DOMSource重用已经加载的XML文档来创建转换器.您可以在下面找到代码(简化后;通常,样式表文档来自其他地方):

I'm having a strange problem and can't seem to find any solution. I'm simply trying to apply an XSLT stylesheet to an XML file (in this case, SPARQL query results formatted as XML, but any other XML file gives the same result). In this case, I need to create the transformer by re-using an already loaded XML document, via DOMSource. You can find the code below (simplified; normally, the stylesheet document comes from elsewhere):

TransformerFactory factory = TransformerFactory.newInstance();
Document stylesheet = db.parse(new File("C:/workspace_5/stylesheet.xml"));
Transformer xformer = factory.newTransformer(new DOMSource(stylesheet));

Source source = new StreamSource(new FileInputStream("C:/workspace_5/xml-file.xml"));        
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Result result = new StreamResult(bOut);

xformer.transform(source, result);

StreamResult的输出流返回转换后的样式表,而不是转换后的XML.如果我将第二行和第三行替换为:

Instead of the transformed XML, the StreamResult's outputstream returns the loaded stylesheet. If I replace the second and third line by:

Transformer xformer = factory.newTransformer(new StreamSource(
    new FileInputStream(""C:/workspace_5/stylesheet.xml")));

然后一切正常.是的,当然,我可以将加载的样式表文档序列化为字符串,将其转换为ByteArrayInputStream(或更糟糕的是,将其写入文件),然后使用它来创建StreamSource,但这很愚蠢.

Then everything works just fine. And yes, of course I could serialize the loaded stylesheet document into a string, convert it to a ByteArrayInputStream (or even worse, write it to a file), and then use that to create a StreamSource, but that's just silly.

是否有任何不起作用的原因?

Is there any reason this is not working?

xml-file.xml代码:

The xml-file.xml code:

<?xml version="1.0"?>
<sparql>
  <head>
    <variable name="buyerName"/>
  </head>
  <results>
    <result>
      <binding name="buyerName">
        <literal>John Doe</literal>
      </binding>
    </result>
  </results>
</sparql>

stylesheet.xml代码:

The stylesheet.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:po="http://www.w3.org/2002/ws/sawsdl/spec/wsdl/order#"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:template match="/sparql">
    <xsl:for-each select="results/result">

        <buyer xsi:type="po:Buyer">
            <name xsi:type="string"><xsl:value-of select="binding[@name='buyerName']/literal" /></name>
        </buyer>

    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

谢谢!

威廉

推荐答案

您的DocumentBuilderFactory很可能不支持名称空间.尝试使用以下方法打开名称空间意识:

Your DocumentBuilderFactory is most likely not namespace aware. Try turning on namespace awareness with:

factory.setNamespaceAware(true);

您可以查看此帖子以获取相关代码.

You can check out this post for the related code.

这篇关于通过DOMSource的JAXP Transformer返回样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆