有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java XML修复命名空间声明

我试图检测/解决RSS元素中的thisbug。 这意味着我必须找到一个错误的名称空间声明并更改其名称空间 值设置为正确的命名空间。例如:

xmlns:media="http://search.yahoo.com/mrss" 

必须是:

xmlns:media="http://search.yahoo.com/mrss/" 

在给定组织的情况下,我如何实现这一目标。w3c。文件

我的意思是我们发现了如何获取某个名称空间的所有元素:

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        XPathExpression expr = xpath.compile("//*[namespace-uri()='http://search.yahoo.com/mrss']");


        Object result = expr.evaluate(d, XPathConstants.NODESET);
        if (result != null) {
            NodeList nodes = (NodeList) result;
            for(int node=0;node<nodes.getLength();node++)
            {
                Node n = nodes.item(node);
                this.log.warn("Found old mediaRSS namespace declaration: "+n.getTextContent());
            }

        } 

所以现在我必须弄清楚如何通过JAXP更改节点的名称空间


共 (2) 个答案

  1. # 1 楼答案

    您可能可以使用XSLT,使用如下规则:

    <xsl:template match="media:*">
       <xsl:element name="local-name()" namespace="http://search.yahoo.com/mrss/">
          <xsl:apply-templates match="node()|@*"/>
       </xsl:element>
    </xsl:template>
    

    其中媒体绑定到“http://search.yahoo.com/mrss

    你可能需要稍微调整一下语法,因为我在没有编译器的帮助下写这篇文章。此外,您可能不会得到非常好的格式(许多元素上的名称空间声明),但它应该在位置上是正确的

  2. # 2 楼答案

    为了完整起见:

    Java代码:

    Document d = out.outputW3CDom(converted);
                DOMSource oldDocument = new DOMSource(d);
                DOMResult newDocument = new DOMResult();
                TransformerFactory tf = TransformerFactory.newInstance();
                StreamSource xsltsource = new StreamSource(
                        getStream(MEDIA_RSS_TRANSFORM_XSL));
                Transformer transformer = tf.newTransformer(xsltsource);
                transformer.transform(oldDocument, newDocument);
    
    private InputStream getStream(String fileName) {
        InputStream xslStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("/" + fileName);
        if (xslStream == null) {
            xslStream = Thread.currentThread().getContextClassLoader()      .getResourceAsStream(fileName);
            }
            return xslStream;
        }
    

    样式表:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <! identity transform that will copy matched node/attribute to the output and apply templates for it's children and attached attributes >
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="@*|*|text()" />
            </xsl:copy>
        </xsl:template>
    
        <! Specialized template to match on elements with the incorrect namespace and generate a new element >
        <xsl:template match="//*[namespace-uri()='http://search.yahoo.com/mrss']">
            <xsl:element name="{local-name()}" namespace="http://search.yahoo.com/mrss/" >
                <xsl:apply-templates select="@*|*|text()" />
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    特别感谢Mads Hansen对XSLT的help