2012-09-12 5 views
2

는 다음과 같이 입력 XML 문서를 감안할 때 :변환 '임베디드'XSLT에서 CDATA 출력에 XML의 문서는 (1.0)

<?xml version="1.0" encoding="utf-8"?> 
<title> This contains an 'embedded' HTML document </title> 
<document> 
<html> 
<head><title>HTML DOC</title></head> 
<body> 
Hello World 
</body> 
</html> 
</document> 
</root> 

내가 그 '내부'HTML 문서를 추출 할 수있는 방법; CDATA로 렌더링하고 출력 문서에 포함 시키시겠습니까?

따라서 출력 문서는 HTML 문서가됩니다. 이 요소에는 요소를 텍스트로 보여주는 텍스트 상자가 있습니다 (내부 문서의 '소스보기'를 표시 할 것입니다).

나는 이것을 시도 :

<xsl:template match="document"> 
<xsl:value-of select="*"/> 
</xsl:template> 

그러나 이것은 단지 텍스트 노드를 렌더링합니다.

나는이 시도 :

<xsl:template match="document"> 
<![CDATA[ 
<xsl:value-of select="*"/> 
]]> 
</xsl:template> 

을하지만 실제 XSLT를 탈출 내가 얻을 :

<xsl:output method="xml" indent="yes" cdata-section-elements="document"/> 
[...] 
<xsl:template match="document"> 
<document> 
<xsl:value-of select="*"/> 
</document> 
</xsl:template> 

이것은 CDATA 삽입 않습니다

&lt;xsl:value-of select="*"/&gt; 

나는 이것을 시도 섹션에 있지만 출력에는 여전히 텍스트 (제거 된 요소) 만 포함됩니다.

<?xml version="1.0" encoding="UTF-8"?> 
<html> 
    <head> 
     <title>My doc</title> 
    </head> 
    <body> 
     <h1>Title: This contains an 'embedded' HTML document </h1> 
     <document><![CDATA[ 
               HTML DOC 

                   Hello World 

           ]]></document> 
    </body> 
</html> 
+0

당신이 보여줄 수 귀하의 예상 출력 : xsl:output에서 cdata-section-elements 속성을 삭제하고 함께 document 요소에 대한 템플릿을 대체? –

답변

11

여기서 해결해야 할 두 가지 혼란이 있습니다.

먼저 xsl:value-of이 아닌 xsl:copy-of이 필요합니다. 후자는 요소의 문자열 값을 반환하고, 전자는 요소의 복사본을 반환합니다.

둘째, xsl:outputcdata-section-elements 속성은 요소 및 특성이 아닌 텍스트 노드의 직렬화에 영향을줍니다. 읽기를 중지

<xsl:template match="document/descendant::*"> 
    <xsl:value-of select="concat('&lt;', name())"/> 
    <!--* attributes are left as an exercise for the reader ... *--> 
    <xsl:text>&gt;</xsl:text> 
    <xsl:apply-templates/> 
    <xsl:value-of select="concat('&lt;/', name(), '>')"/> 
</xsl:template> 

그러나 빠른 방법은 다음과 같은 솔루션과 같은 (점잔 빼는 독자 것 : 한 가지 방법은 다음과 같은 (테스트하지)의 라인을 따라, 당신은 HTML을 직접 직렬화하는 것입니다 원하는 것을 얻을 수 있습니다 지금), 나의 친구 Tommie Usdin에 의해 나에게 지적했다. 제발

<xsl:template match="document"> 
    <document> 
    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
    <xsl:copy-of select="./html"/> 
    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
    </document> 
</xsl:template> 
+0

좋은 점 - 나는 < >을 여기에 코딩 할 필요가 있음을 깨닫기 시작했으나 이전에는 disable-output-escaping 옵션을 보지 못했습니다. 사실 각기 다른 괄호를 사용하여 다른 문자 (프랑스어 따옴표!)를 사용했습니다. 부가적인 팁 : re-of-copy-of도 부탁드립니다. 건배에 감사합니다 – monojohnny

+0

. 분명히 끔찍한 독자는 아닙니다. 또는 나는 말해야한다, 우아함에 그것들. hhhheee –

+0

몇 가지 이유로 Saxon 9는 '<! [CDATA]'를 어쨌든 탈출해야한다고 주장합니다. 최소한 이것이 내 XSLT에서하는 것입니다. https://github.com/gioele/rng-doc/blob/600d09759bf1158e132b89c0e6adaad7c5be0bb2/rng-doc.xsl#L131 – gioele