3

我正在尝试解析一个非常简单的示例:

<?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 属性提供了一个空值,而不是预期的结果。但是,当我将 value-of 更改为此:

                <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();
4

1 回答 1

5

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

  1. 在样式表中添加“openSearch”命名空间(从 xml 复制)
  2. 删除“atom”命名空间,因为没有关于此命名空间的信息
  3. 将工厂设置为命名空间感知: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(); 
  } 
于 2013-02-13T05:36:58.977 回答