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"><![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</xsl:template>
를이 생산 :
<?xml version="1.0" encoding="UTF-8"?>
Ulysses
<Author><![CDATA[
<b>Joyce</b>]]></Author>
당신이 도와 주실 래요? 원본 문서를 전체적으로 작성하지만 작성자 요소 내의 모든 것을 둘러싼 CDATA로 작성하고 싶습니다. 감사
XSLT 3.0
같이 : 이 뭔가를 시도 할 수 Jsoup를 사용하십니까? 마크 업이있는 XML은 적절한 XML이 아닙니다. 당신은 그 마법의 문자들을 벗어나거나 CDATA로 감쌀 수 있습니다. 다른 선택은 없습니다. – duffymo