微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javax.xml.transform.Transformer忽略前缀?

我试图解析一个非常简单的例子:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'>
    <openSearch:totalResults>100</openSearch:totalResults>
</root>

我使用的样式表如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0" 
                xmlns:app='http://www.w3.org/2007/app' >
<xsl:output method="xml" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="/">
    <results>
        <xsl:attribute name="total-results">
                <xsl:value-of 
                 select="atom:root/openSearch:totalResults"/>
        </xsl:attribute>
    </results>
</xsl:template>
</xsl:stylesheet>

这在libxslt中工作,没问题.我现在尝试在java中执行相同的任务,我正在尝试使用javax.xml.transform包来执行此操作.它不是预期的结果,而是为total-results属性提供一个空值.但是,当我将值更改为:

                <xsl:value-of select="root/totalResults"/>

有用.更改xml和xslt不是一个选项.我应该在某个地方设置一个参数吗?代码非常简单:

InputSource xmlSource = new InputSource( new StringReader(xml) );

DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xmlSource);

// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();


StreamSource stylesource = new StreamSource(new StringReader(styleSheet));
Transformer transformer = tFactory.newTransformer(stylesource);

StringWriter writer = new StringWriter();

DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);

stringResult = writer.toString();

解决方法:

在样式表中,您缺少“atom”和“openSearch”的名称空间声明.
以下作品:

>在样式表中添加“openSearch”命名空间(从xml复制)
>删除“atom”命名空间,因为没有关于此的信息
命名空间
>将工厂设置为名称空间感知:factory.setNamespaceAware(true);

这是Scala中的完整代码(抱歉,我懒得从文件解析xml和样式表或在Java中进行字符串连接):

  def testxsl = {
      val xml = """<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'>
    <openSearch:totalResults>100</openSearch:totalResults>
</root>
      """
      val styleSheet = """<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
        xmlns:app='http://www.w3.org/2007/app' 
        xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'>
<xsl:output method="xml" indent="yes"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="/">
    <results>
        <xsl:attribute name="total-results">
                <xsl:value-of select="root/openSearch:totalResults"/>
        </xsl:attribute>
    </results>
</xsl:template>
</xsl:stylesheet>
        """
    val xmlSource = new InputSource( new StringReader(xml) );
    val factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    val builder = factory.newDocumentBuilder();
    val document = builder.parse(xmlSource);

    // Use a Transformer for output
    val tFactory = TransformerFactory.newInstance();


    val stylesource = new StreamSource(new StringReader(styleSheet));
    val transformer = tFactory.newTransformer(stylesource);

    val writer = new StringWriter();

    val source = new DOMSource(document);
    val result = new StreamResult(writer);
    transformer.transform(source, result);
    writer.toString(); 
  } 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。