2010-03-14 9 views
1

RSS 요소에 this 버그를 피하려고합니다. 즉, 잘못된 네임 스페이스 선언을 찾아서 값을 올바른 네임 스페이스로 변경해야합니다. 예컨대 :XML 수정 네임 스페이스 선언

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

은 다음과 같아야합니다

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

은 어떻게 org.w3c.Document을 부여하는 것이 achive 수 있습니까? 그래서 지금

 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를 통해 노드의 네임 스페이스를 변경하는 방법을 파악해야 :

나는 특정 네임 스페이스의 모든 요소를 ​​얻는 방법을 발견 meanwile.

답변

0

:

자바 코드 :

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

스타일 시트 다음 XSLT와의 help에 대한 매즈 한센에

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

특별 감사합니다.

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"에 바인딩된다.

컴파일러의 도움없이이 글을 쓰고 있기 때문에 약간의 구문을 조정해야 할 수도 있습니다. 또한, 당신이 얻는 것은 매우 멋지게 포맷 된 것일 것입니다 (많은 요소들에 대한 네임 스페이스 선언), 그러나 그것은 반드시 정확해야합니다. 그냥 완성도를 위해서

+0

답장을 보내 주셔서 감사합니다. 그러나 개체 수준에서 문서에 액세스하고 있습니다. 또한 로컬 접두사가 항상 "미디어 :"인지 여부도 확실하지 않습니다. 결국 이것은 다른 사람들이 만든 RSS 피드입니다. 하나님은 그들이 사용하는 접두어를 알고 계십니다. -/ – er4z0r

+0

그들은 꼭 가질 필요가 없습니다! XSLT의 모든 접두사 (예 : 'x : *')를 사용할 수 있으며 중요한 것은 모두 네임 스페이스입니다. 즉, XSLT에서 사용하는 접두사는 XML 파일의 접두어와 아무 관련이 없습니다. –

+0

@ er4z0r - XSLT (즉 미디어)에 선언 한 네임 스페이스 접두사는 소스 문서의 네임 스페이스 접두사. 둘 다 동일한 URI를 참조하는 한 템플릿이 일치합니다. –