2017-10-11 3 views
-1

HTML 요소와 XML을 구문 분석이 필요 구문 분석 할 때 html 또는 가능한 사용자 정의 xml 태그가 xml 요소가 아닌 일반 텍스트로 유지됩니다.나는 다음과 같은 xml 파일이

XML을 전혀 편집 할 수 없지만 xml을 변환 할 사용자 지정 xslt 파일을 만들면 괜찮습니다.

는 는

나는이 기록됩니다 파일에 내 XSLT를 적용 되는가

TransformerFactory factory = TransformerFactory.newInstance(); 
    Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile()); 
    Transformer transformer = factory.newTransformer(stylesheetSource); 
    Source inputSource = new StreamSource(new File(inputPathname).getAbsoluteFile()); 
    Result outputResult = new StreamResult(new File(outputPathname).getAbsoluteFile()); 
    transformer.transform(inputSource, outputResult); 

는 XML의 읽기를 지원하기 위해 XSLT를 사용하는 다음 Java 코드는, 그러나 나는 가지고 올 수없는 것 그것을 할 올바른 xslt 함께. 나는 Add CDATA to an xml file을 보았지만 이것이 나에게 도움이되지 않습니다.

기본적으로, 나는 파일이 그럼 난 "James <b>Joyce</b>"을 추출 할 수 있습니다

<?xml version="1.0"?> 
<Book> 
    <Title>Ulysses</Title> 
    <Author><![CDATA[James <b>Joyce</b>]]></Author> 
</Book> 

같이 할 생각합니다. 나는 여기에 제안 된 접근 방식을 시도 : Add CDATA to an xml file 하지만 그것은 나를 위해 작동하지 않았다.

나는 다음과 같은 XSLT 사용 :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 

<xsl:template match="Author"> 
<xsl:copy> 
<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
<xsl:copy-of select="*"/>  
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
</xsl:copy> 
</xsl:template> 

를이 생산 :

<?xml version="1.0" encoding="UTF-8"?> 
    Ulysses 
    <Author><![CDATA[ 
<b>Joyce</b>]]></Author> 

당신이 도와 주실 래요? 원본 문서를 전체적으로 작성하지만 작성자 요소 내의 모든 것을 둘러싼 CDATA로 작성하고 싶습니다. 감사

XSLT 3.0
+1

같이 : 이 뭔가를 시도 할 수 Jsoup를 사용하십니까? 마크 업이있는 XML은 적절한 XML이 아닙니다. 당신은 그 마법의 문자들을 벗어나거나 CDATA로 감쌀 수 있습니다. 다른 선택은 없습니다. – duffymo

답변

0

(메이븐 및 소스 포지에 사용 가능) 색슨 9.8 HE에서 지원하는 다음과 같이 XSLT를 사용할 수 있습니다

당신의 시도에 대해서는
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" 
    exclude-result-prefixes="xs math" 
    version="3.0"> 

    <xsl:output cdata-section-elements="Author"/> 

    <xsl:mode on-no-match="shallow-copy"/> 

    <xsl:template match="Author"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:value-of select="serialize(node())"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

, 당신은 기본적으로 신원을 "구현"필요 간결 주형으로 <xsl:mode on-no-match="shallow-copy"/> XSLT 3.0로 작성된 변환 템플릿

그 노드 Author 요소 것과 같은 (보다 특수한 서식 처리되지되도록 XSLT 1.0
<xsl:template match="@* | node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

s)가 재귀 적으로 복사됩니다.

그런 다음, 함께 Jsoup처럼이 문제를 해결하는 더 좋은 방법을 간단한 HTML/XML 파서를 사용하지 않는 모든 자식 노드 node() 당신이

<xsl:template match="Author"> 
<xsl:copy> 
<xsl:apply-templates select="@*"/> 
<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
<xsl:copy-of select="node()"/>  
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
</xsl:copy> 
</xsl:template> 
+0

XSLT 1.0 예제를 시도했지만 일부 줄 바꿈 차이가있는 것을 제외하고는 예상대로 작동합니다.이 문제를 해결하는 방법에 대한 아이디어가 있습니까? 이 코멘트에서 입력/출력을 추가하려고했지만 잘 표시되지 않습니다. 답변 해 주셔서 감사합니다. 정말 고맙습니다. –

+0

'xsl : output''indent = "yes"'의 유무와 관계없이 발생합니까? 공백이 보존되어야한다면 나는 시도 할 것이다. –

+0

나는 indent = "no"로 설정하고 모두 제거했지만 결과는 같았다. –

0

를 얻을 수 *뿐만 아니라 요소 노드를 선택 복사의 ?

"나를 위해 작동하지 않았다"무엇
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.parser.Parser; 
import org.jsoup.select.Elements; 

public class Example { 

    public static void main(String[] args) { 
     String xml = "<?xml version=\"1.0\"?>\n" 
       + "<Book>\n" 
       + " <Title>Ulysses</Title>\n" 
       + " <Author>James <b>Joyce</b></Author>\n" 
       + "</Book>"; 
     Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); 
     doc.outputSettings().prettyPrint(false); 
     Elements books = doc.select("Book"); 
     for(Element e: books){ 
      Book b = new Book(e.select("Title").html(),e.select("Author").html()); 
      System.out.println(b.title); 
      System.out.println(b.author); 
     } 
    } 
    public static class Book{ 
     String title; 
     String author; 

     public Book(String title, String author) { 
      this.title = title; 
      this.author = author; 
     }   
    } 
} 
+0

답변 해 주셔서 감사합니다. 이것은 내가 원하는 것을 얻을 수 있지만이 경우에 더 많은 HTML을 추가하면 Jsoup에 의해 파싱 될 때 변경됩니다. 필요한 요소는 변경되지 않고 복제 될 정확한 텍스트입니다. –

관련 문제