Question

Je suis en train de detetct / travail autour ce bug éléments RSS. Cela signifie que je dois trouver un mauvais déclaration_namespace et changer son valeur à l'espace de noms correct. Par exemple:

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

doit être:

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

Comment puis-je achive qui donne un org.w3c.Document?

Je Meanwile trouvé comment obtenir tous les éléments d'un certain espace de noms:

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

        } 

Alors maintenant, je dois comprendre comment changer l'espace de noms d'un nœud via JAXP.

Était-ce utile?

La solution 2

Juste pour être complet:

code 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;
    }

Stylesheet:

<?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>

Merci à Mads Hansen pour son aider avec le XSLT.

Autres conseils

Vous pouvez probablement le faire avec XSLT, avec une règle comme ceci:

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

où les médias est lié à " http://search.yahoo.com/mrss".

Vous devrez peut-être modifier la syntaxe un peu, comme je l'écris sans l'aide d'un compilateur. De plus, ce que vous obtiendrez est probablement pas très bien formaté (déclarations d'espace de noms sur de nombreux éléments), mais il devrait être locically correcte.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top