dichiarazione di correzione namespace XML
-
20-09-2019 - |
Domanda
Sto cercando di detetct / lavoro intorno questo bug elementi RSS. Ciò significa che devo trovare una sbagliata namespace-dichiarazione e cambiare il suo il valore allo spazio dei nomi corretto. Per esempio:
xmlns:media="http://search.yahoo.com/mrss"
deve essere:
xmlns:media="http://search.yahoo.com/mrss/"
Come posso achive che, dato un org.w3c.Document?
I meanwile scoperto come ottenere tutti gli elementi di un certo namespace:
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());
}
}
Così ora devo capire come cambiare lo spazio dei nomi di un nodo tramite JAXP.
Soluzione 2
Solo per ragioni di completezza:
del codice 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;
}
foglio di stile:
<?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>
Un ringraziamento speciale a Mads Hansen per il suo aiutare con l'XSLT.
Altri suggerimenti
Probabilmente potrebbe fare con XSLT, con una regola come questa:
<xsl:template match="media:*">
<xsl:element name="local-name()" namespace="http://search.yahoo.com/mrss/">
<xsl:apply-templates match="node()|@*"/>
</xsl:element>
</xsl:template>
in cui i media è destinata a " http://search.yahoo.com/mrss ".
Potrebbe essere necessario modificare la sintassi un po ', come io sto scrivendo questo senza l'aiuto di un compilatore. Inoltre, ciò che si otterrà probabilmente non è estremamente ben formattata (dichiarazioni di namespace su molti elementi), ma dovrebbe essere locically corrette.