2013-05-31 3 views
1

다음과 같은 매우 큰 xml 문서가 있습니다. 다음 발췌 내용과 같습니다. 웹에서 찾은 몇 가지 예제를 사용하여 파일을 여러 HTML 파일로 분할 할 수 있습니다.xslt를 사용하여 xml을 여러 HTML 파일로 분할

결과 파일의 유일한 문제점은 <h1> 태그와 다음 요소 앞에 모든 요소를 ​​포함하고 다음 요소가있는 다음 <h1> 등을 가져야한다는 것입니다. 다음 H1 태그에 포함되지 않습니다 전에 그러니까 기본적으로 내가 필요한

은 ( <p>, <ol> <pre>) 다음과 같은 요소와 함께 <h1 id=h1>에 대한 파일은 다음과 같은 요소를 만들어 현재 을 파일을 생성 할 수있다 생성 된 문서 그리고 나는 그것을하기 위해 xslt를 조절하는 방법을 모른다. 여기에 IBM 개발자 웹 사이트에서 다음 예제 원본 XML

<?xml version="1.0" encoding="UTF-8"?> 

<paragraphs> 

<h1 id= "h1">Header One</h1> 

<p>The quick brown fox jumps over the lazy dog. </p> 

<p>The quick brown fox jumps over the lazy dog. </p> 

<p>The quick brown fox jumps over the lazy dog.</p> 
<ol> 
    <li> 
     List 1 
     </li> 
     <li> 
      List 2 
      </li> 

</ol> 

      <h1 id= "h2">Header Two</h1> 

      <p>The quick brown fox jumps over the lazy dog. </p> 

      <p>The quick brown fox jumps over the lazy dog.</p> 
      <ul> 
       <li> 
        List 3 
        </li 
       > 
        <li> 
         List 4 
         </li> 

      </ul> 

      <p>The quick brown fox jumps over the lazy dog.</p> 

      <h1 id= "h3">Header Three</h1> 

      <pre>my example one</pre> 

      <p>The quick brown fox jumps over the lazy dog.</p> 

      <pre> Another example</pre> 

</paragraphs> 

XSLT를 :

<html> 
    <body>Header One</body> 
    </html> 

<html> 
    <body>Header Two</body> 
    </html> 
: http://www.ibm.com/developerworks/library/x-tipmultxsl/

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

    <xsl:output method="text"/> 
    <xsl:output method="html" indent="yes" name="html"/> 

    <xsl:template match="/"> 
     <xsl:for-each select="//h1"> 
      <xsl:variable name="filename" 
       select="concat('output/',@id,'.html')" /> 
      <xsl:value-of select="$filename" /> <!-- Creating --> 
      <xsl:result-document href="{$filename}" format="html"> 
       <html><body> 
        <xsl:value-of select="text()"/> 
       </body></html> 
      </xsl:result-document> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

결과 파일은 다음과 같이


<html> 
    <body>Header Three</body> 
    </html> 

고마워요. 난 당신이 이런 식으로 뭔가를 찾고 생각

+0

을, 나와 함께 곰하시기 바랍니다. – ManUO

답변

1

...

XML 입력

<paragraphs> 
    <h1 id="h1">Header One</h1> 
    <p>The quick brown fox jumps over the lazy dog. </p> 
    <p>The quick brown fox jumps over the lazy dog. </p> 
    <p>The quick brown fox jumps over the lazy dog.</p> 
    <ol> 
     <li> List 1 </li> 
     <li> List 2 </li> 
    </ol> 
    <h1 id="h2">Header Two</h1> 
    <p>The quick brown fox jumps over the lazy dog. </p> 
    <p>The quick brown fox jumps over the lazy dog.</p> 
    <ul> 
     <li> List 3 </li> 
     <li> List 4 </li> 
    </ul> 
    <p>The quick brown fox jumps over the lazy dog.</p> 
    <h1 id="h3">Header Three</h1> 
    <pre>my example one</pre> 
    <p>The quick brown fox jumps over the lazy dog.</p> 
    <pre> Another example</pre> 
</paragraphs> 

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*"> 
     <xsl:for-each-group select="*" group-starting-with="h1"> 
      <xsl:result-document href="output/{@id}.html"> 
       <html> 
        <body> 
         <xsl:copy-of select="current-group()"/> 
        </body> 
       </html> 
      </xsl:result-document> 
     </xsl:for-each-group> 
    </xsl:template> 

</xsl:stylesheet> 

출력 파일

h1.html

<html> 
    <body> 
     <h1 id="h1">Header One</h1> 
     <p>The quick brown fox jumps over the lazy dog. </p> 
     <p>The quick brown fox jumps over the lazy dog. </p> 
     <p>The quick brown fox jumps over the lazy dog.</p> 
     <ol> 
     <li> List 1 </li> 
     <li> List 2 </li> 
     </ol> 
    </body> 
</html> 

h2.html

<html> 
    <body> 
     <h1 id="h2">Header Two</h1> 
     <p>The quick brown fox jumps over the lazy dog. </p> 
     <p>The quick brown fox jumps over the lazy dog.</p> 
     <ul> 
     <li> List 3 </li> 
     <li> List 4 </li> 
     </ul> 
     <p>The quick brown fox jumps over the lazy dog.</p> 
    </body> 
</html> 

또한 h3.html

<html> 
    <body> 
     <h1 id="h3">Header Three</h1> 
     <pre>my example one</pre> 
     <p>The quick brown fox jumps over the lazy dog.</p> 
     <pre> Another example</pre> 
    </body> 
</html> 

다른 변환 작업을 수행해야하는 경우, 당신이 할 수있는 항등 변환을 추가하고 xsl:copy-of 대신 xsl:apply-templates을 사용하십시오. 그런 다음 필요에 따라 추가 템플릿을 추가 할 수 있습니다.당신이 ul 모든 ol 요소를 변경하고자한다면

예를 들어, 당신이 할 것입니다 : 내가 제대로 메시지를 포맷 할 수없는 나는 몇 가지 이유에 대한

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="/*"> 
     <xsl:for-each-group select="*" group-starting-with="h1"> 
      <xsl:result-document href="output/{@id}.html"> 
       <html> 
        <body> 
         <xsl:apply-templates select="current-group()"/> 
        </body> 
       </html> 
      </xsl:result-document> 
     </xsl:for-each-group> 
    </xsl:template> 

    <xsl:template match="ol"> 
     <ul> 
      <xsl:apply-templates select="@*|node()"/> 
     </ul> 
    </xsl:template> 

</xsl:stylesheet> 
+0

감사합니다. @ daniel-haley, 예상대로 작동했습니다. – ManUO

관련 문제