开发者

Possible to split XML into multiple pages with XSLT?

开发者 https://www.devze.com 2023-02-07 15:39 出处:网络
I have an XML document with pagination indicated. I would like to to transform this single file into multiple output files. Th开发者_JAVA百科at is, I would like to convert the following:

I have an XML document with pagination indicated. I would like to to transform this single file into multiple output files. Th开发者_JAVA百科at is, I would like to convert the following:

<root>
   <page>
     Page 1 contents
   </page>
   <page>
     Page 2 contents
   </page>
</root>

To

page1.html

<html>
  <head></head>
  <body>
    Page 1 contents
  </body>
</html>

page2.html

<html>
  <head></head>
  <body>
    Page 2 contents
  </body>
</html>


Possible to split XML into multiple pages with XSLT?

With XSLT 2.0 -- yes use the <xsl:result-document> element:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="page">
  <xsl:result-document href="page{position()}.html">
    <html>
        <head></head>
        <body><xsl:value-of select="."/></body>
    </html>
  </xsl:result-document>
 </xsl:template>
</xsl:stylesheet>

Results:

Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_23
Stylesheet compilation time: 549 milliseconds
Processing file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
Building tree for file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml using class net.sf.saxon.tinytree.TinyBuilder
Tree built in 4 milliseconds
Tree size: 7 nodes, 30 characters, 0 attributes
Loading net.sf.saxon.event.MessageEmitter
Writing to file:/C:/Program%20Files/Java/jre6/bin/page1.html
Writing to file:/C:/Program%20Files/Java/jre6/bin/page2.html
Execution time: 86 milliseconds
Memory used: 11480728
NamePool contents: 23 entries in 22 chains. 6 prefixes, 7 URIs

With XSLT 1.0 one may use the EXSLT extension element <exslt:document>:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="page">
  <ext:document href="page{position()}.html">
    <html>
        <head></head>
        <body><xsl:value-of select="."/></body>
    </html>
  </ext:document>
 </xsl:template>
</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消