2013-04-05 2 views
2

우리는 HTML 페이지이지만 추가 XML 요소 (모두 회사 이름이 'TLA'임)가있는 파일 묶음을 가지고 있으며 이전 프로그램에 대한 데이터와 구조를 제공합니다. 지금 재 작성.혼합 된 xml과 HTML에서 HTML 만 복사합니다.

예 양식 :

<html > 
<head> 
    <title>Highly Simplified Example Form</title> 
</head> 
<body> 
    <TLA:document xmlns:TLA="http://www.tla.com"> 
     <TLA:contexts> 
      <TLA:context id="id_1" value=""></TLA:context> 
     </TLA:contexts> 
     <TLA:page> 
      <TLA:question id="q_id_1"> 
       <table> 
        <tr> 
         <td> 
          <input id="input_id_1" type="text" /> 
         </td> 
        </tr> 
       </table> 
      </TLA:question> 
     </TLA:page> 
     <!-- Repeat many times --> 
    </TLA:document> 
</body> 
</html> 

내 작업은 새 파일에 해당 속성과 콘텐츠를 갖춘 단지 HTML 요소를 복사합니다 사전에 프로세서를 작성하는 것입니다. 이처럼

:

나는 그것이 내가 다른 파일에 대한 extract the TLA elements에 필요한 무엇 이었습니까으로 XSLT를 사용하는 접근 방식을 촬영했습니다
<html > 
<head> 
    <title>Highly Simplified Example Form</title> 
</head> 
<body> 
    <table> 
     <tr> 
      <td> 
       <input id="input_id_1" type="text" /> 
      </td> 
     </tr> 
    </table> 
    <!-- Repeat many times --> 
</body> 
</html> 

. 지금까지 내가 가지고있는 XSLT입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:mbl="http://www.mbl.com"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*" /> 
    <xsl:template match="mbl:* | mbl:*/@* | mbl:*/text()"/> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template>  
</xsl:stylesheet> 

그러나 이것은 단지 다음과 생산 : 문서 요소가 제외 :

<html > 
<head> 
    <title>Highly Simplified Example Form</title> 
</head> 
<body> 
</body> 
</html> 

당신이 TLA 내에서 모든 것을 볼 수 있듯이. HTML을 모두 가져 오지만 TLA 요소를 필터링하려면 XSLT에서 무엇을 변경해야합니까?

또는 이와 관련하여 더 간단한 방법이 있습니까? 거의 모든 브라우저가 TLA 요소를 무시하므로 HTML 도구 나 응용 프로그램을 사용하여 필요한 것을 얻을 수있는 방법이 있습니까?

답변

1

특히 어려울 것이다 HTML 요소를 대상으로하지만, 당신은 단지 TLA 네임 스페이스의 내용을 제외 할 (여전히 TLA 요소를 포함하는 것이 아닌 TLA 요소를 포함)하는 경우, 다음이 작동합니다 :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:mbl="http://www.tla.com" exclude-result-prefixes="mbl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="@*|node()" priority="-2"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- This element-only identity template prevents the 
     TLA namespace declaration from being copied to the output --> 
    <xsl:template match="*"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates select="@* | node()" /> 
    </xsl:element> 
    </xsl:template> 

    <!-- Pass processing on to child elements of TLA elements --> 
    <xsl:template match="mbl:*"> 
    <xsl:apply-templates select="*" /> 
    </xsl:template> 
</xsl:stylesheet> 
당신이 무엇이든을 제외 할 경우

또한 대신 사용할 수 있습니다 어떤 null 이외의 네임 스페이스 :

샘플 입력에서 실행 중
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:mbl="http://www.tla.com" exclude-result-prefixes="mbl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="@*|node()" priority="-2"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates select="@* | node()" /> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="*[namespace-uri()]"> 
    <xsl:apply-templates select="*" /> 
    </xsl:template> 
</xsl:stylesheet> 

, 결과는 다음과 같습니다

<html> 
    <head> 
    <title>Highly Simplified Example Form</title> 
    </head> 
    <body> 
    <table> 
     <tr> 
     <td> 
      <input id="input_id_1" type="text" /> 
     </td> 
     </tr> 
    </table> 
    </body> 
</html> 
+0

올바른 HTML을 출력하지 않는다는 점에서 사소한 문제점이 있습니다. 자동 닫는 입력 요소에 주목하십시오. 이는 xhtml로만 유효합니다. 거기에 유효한 HTML을 얻을 수있는 방법은 실제 문서 (xsl : 출력 HTML을 사용하여)에 대해 이것을 실행하면 많은 태그가 공개되지 않은 떠난다? –

+0

귀하의 의견 중 두 번째 문장을 이해하지 못합니다. html로 출력하고 싶다면,'xsl : output' 메소드를 "html"로 바꿀 수 있습니다. – JLRishe

+0

전체 질문을하는 것이 더 나을 것이라고 생각했습니다. http://stackoverflow.com/questions/15897500/closing-tags-when-extracting-html-from-xml –

관련 문제