2015-01-22 3 views
0

파일의 구분자로 h1 노드를 사용하여 HTML 파일을 여러 HTML 파일로 분할해야합니다.
Exemple는 :XSL 노드에서 노드로 파일 분할

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <p><span>This is my frontpage</span></p> 
      <div><img src="images/frontpage.png" width="100" height="50" style="border:none" /></div> 
     </div> 
     <div> 
      <h1> Title 1 </h1><p> some blabla for title_1 </p> 
      <h2> Title 1.1 </h2><p> some blabla for title_1_1 </p><img src="images/title_1_1.png" width="50" height="50"/> 
      <h1> Title 2 </h1><p> some blabla for title_2 </p> 
     </div> 
     <div> 
      <p> other blabla </p> 
      <h1> Title 3 </h1><p> some blabla for title_3 </p> 
     </div> 
    </body> 
</html> 

나는 4 개 출력을 할 수 있습니다.

frontpage.html :

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <p><span>This is my frontpage</span></p> 
      <div><img src="images/frontpage.png" width="100" height="50" style="border:none" /></div> 
     </div> 
    </body> 
</html> 

output1.html :

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <h1> Title 1 </h1><p> some blabla for title_1 </p> 
      <h2> Title 1.1 </h2><p> some blabla for title_1_1 </p><img src="images/title_1_1.png" width="50" height="50"/> 
     </div> 
    </body> 
</html> 

output2.html :

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <h1> Title 2 </h1><p> some blabla for title_2 </p> 
     </div> 
     <div> 
      <p> other blabla </p> 
     </div> 
    </body> 
</html> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <h1> Title 3 </h1><p> some blabla for title_3 </p> 
     </div> 
    </body> 
</html> 
output3.html

이 문제를 해결하기위한 모든 아이디어에 감사드립니다.

PS : 나는 XSLT 2.0 사양하지만, 이전 초안을 구현하지 않는 8.9 전에 XSLT 2.0 및 색슨 8

+1

의사 코드가 아닌 올바른 형식의 XML 샘플을 제공하는 것을 고려하십시오. –

+0

@MartinHonnen 예, 저는 편집기를 사용하여 HTML 코드를 추가하는 방법을 몰랐습니다. 죄송합니다 그 – dgedge03

+0

@ dg99 나는 몇 가지 코드를 시도했지만 그 결과는 결코 내가 예상 한 것입니다. – dgedge03

답변

1

색슨 (8)는 몇 살 것을 주 및 버전을 사용하십시오.

다음 슨 9.6 테스트는 XSLT 2.0 스타일이다 입력 파일

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="Content-Style-Type" content="text/css" /> 
     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
    </head> 
    <body> 
     <div> 
      <p><span>This is my frontpage</span></p> 
      <div><img src="images/frontpage.png" width="100" height="50" style="border:none" /></div> 
     </div> 
     <div> 
      <h1> Title 1 </h1><p> some blabla for title_1 </p> 
      <h2> Title 1.1 </h2><p> some blabla for title_1_1 </p><img src="images/title_1_1.png" width="50" height="50"/> 
      <h1> Title 2 </h1><p> some blabla for title_2 </p> 
     </div> 
     <div> 
      <p> other blabla </p> 
      <h1> Title 3 </h1><p> some blabla for title_3 </p> 
     </div> 
    </body> 
</html> 

이 생성인가

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

<xsl:output method="html" version="4.01" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:for-each-group select="//h1 | //text()[not(ancestor::h1)] | //*[not(*) and not(ancestor::h1)]" group-starting-with="h1"> 
    <xsl:variable name="copy" select="current-group()"/> 
    <xsl:variable name="ancestors" select="$copy/ancestor::*"/> 
    <xsl:variable name="filename" select="if (not(self::h1)) then 'frontpage.html' else concat('output', position() - 1, '.html')"/> 
    <xsl:result-document href="{$filename}"> 
     <xsl:apply-templates select="/*"> 
     <xsl:with-param name="copy" select="$copy"/> 
     <xsl:with-param name="ancestors" select="$ancestors"/> 
     </xsl:apply-templates> 
    </xsl:result-document> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="node()"> 
    <xsl:param name="copy"/> 
    <xsl:param name="ancestors"/> 
    <xsl:choose> 
    <xsl:when test="$copy[. is current()]"> 
     <xsl:copy-of select="."/> 
    </xsl:when> 
    <xsl:when test="$ancestors[. is current()]"> 
     <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates> 
      <xsl:with-param name="copy" select="$copy"/> 
      <xsl:with-param name="ancestors" select="$ancestors"/> 
     </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:when> 
    </xsl:choose> 
</xsl:template> 

<xsl:template match="head"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

</xsl:stylesheet> 

네 출력 파일

<html> 

    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 


     <meta http-equiv="Content-Style-Type" content="text/css"> 

     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
     </head> 

    <body> 

     <div> 

     <p><span>This is my frontpage</span></p> 

     <div><img src="images/frontpage.png" width="100" height="50" style="border:none"></div> 

     </div> 

     <div> 

     </div> 
    </body> 
</html> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 


     <meta http-equiv="Content-Style-Type" content="text/css"> 

     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
     </head> 
    <body> 
     <div> 
     <h1> Title 1 </h1> 
     <p> some blabla for title_1 </p> 

     <h2> Title 1.1 </h2> 
     <p> some blabla for title_1_1 </p><img src="images/title_1_1.png" width="50" height="50"> 

     </div> 
    </body> 
</html> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 


     <meta http-equiv="Content-Style-Type" content="text/css"> 

     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
     </head> 
    <body> 
     <div> 
     <h1> Title 2 </h1> 
     <p> some blabla for title_2 </p> 

     </div> 

     <div> 

     <p> other blabla </p> 

     </div> 
    </body> 
</html> 

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 


     <meta http-equiv="Content-Style-Type" content="text/css"> 

     <title>Test</title> 
     <style type="text/css">body { font-family:Helvetica; font-size:9pt }}</style> 
     </head> 
    <body> 
     <div> 
     <h1> Title 3 </h1> 
     <p> some blabla for title_3 </p> 

     </div> 

    </body> 

</html> 

그래서 난 스타일 시트는 필요에 따라 노드를 분할하고 오른쪽 파일의 컨텐츠를 작성하는 생각, 당신은 공백 제거 및 들여 쓰기 실험해야합니다.

+0

이 답변을 주셔서 감사합니다. 시도해보고 의견을 보내 드리겠습니다. Saxon JAR을 실행하면 Saxonica의 Saxon 8.8J가 표시됩니다. – dgedge03

+0

최신 버전은 Saxon 9.6이며, http://saxon.sourceforge.net/에서 오픈 소스 버전으로 제공됩니다. –

+0

Ok, 시도해 보겠습니다. 나는 나의 진짜 필요와 일치하는 나의 첫번째 필요를 변경한다. (나는 당신의 대답에 적응할 수 있기를 바랐다 ...). – dgedge03

관련 문제