代码之家  ›  专栏  ›  技术社区  ›  er4z0r

XML修复命名空间声明

  •  1
  • er4z0r  · 技术社区  · 16 年前

    我正试着去调查 this rss元素中的错误。 这意味着我必须找到一个错误的名称空间声明并更改它 值设置为正确的命名空间。例如:

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

    必须是:

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

    我怎样才能得到一个org.w3c.document?

    我的意思是要找出如何获取某个名称空间的所有元素:

            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 回复  |  直到 16 年前
        1
  •  1
  •   Chris Lercher    16 年前

    您可能可以使用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
  •  0
  •   Community Mohan Dere    8 年前

    为了完整起见:

    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 help 使用xslt。