gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:15 25 4
gpt4 key购买 nike

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

<?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”的命名空间声明。以下作品:

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

关于javax.xml.transform.Transformer 忽略前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14846447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com