XML 수정 네임스페이스 선언
-
20-09-2019 - |
문제
내가 하려고 detetct/주변의 작업 이 버그에 RSS 요소입니다.는 것을 의미가 있을 찾는 잘못된 네임스페이스-을 선언하고 변경 값이 올바른 네임스페이스가 있습니다.E.g:
xmlns:media="http://search.yahoo.com/mrss"
해야합니다:
xmlns:media="http://search.yahoo.com/mrss/"
할 수 있는 방법을 실현하려는 주 org.w3c.셔서 감사합니다.
I meanwile 하는 방법을 발견하고 모든 요소의 특정 네임스페이스:
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
그냥 완전성을 위해:
자바 코드:
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.
다른 팁
도 할 수 있으로,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".
할 수 있습을 조정할 구문을 조금으로,저는 이것을 쓰지 않고 도움의 컴파일러입니다.또한,당신은 아마 매우 잘 포맷(네임스페이스 선언에서 많은 요소)사용할 수 있지만,locically 정확합니다.
제휴하지 않습니다 StackOverflow