0
votes

I have an XML document. I would like to transform this single file into multiple HTML files. when I'm using ext:document inside <xsl:template match="page/page"> the code is not working what is i am doing wrong?

XML

<root>
<page>
    Page 1 INFO
</page>
<page>
    Page 2 INFO 
    <page>
        Page 3 INFO
    </page>
</page>
<page>
    Page 4 INFO
</page>

My XSLT

<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:template match="root/page/page">
  <ext:document href="pageTwo{position()}.html">
    <html>
        <head></head>
        <body><xsl:value-of select="."/></body>
    </html>
  </ext:document>
 </xsl:template>
</xsl:stylesheet>

To

page1.html

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

page2.html

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

page3.html

<html>
  <head></head>
  <body>
    Page 3 INFO
  </body>
</html>

page4.html

<html>
  <head></head>
  <body>
    Page 4 INFO
  </body>
</html>
1
1. Show us your code. 2. Explain exactly what happens when you try it. " the code is not working" is not a good description. - michael.hor257k
@michael.hor257k It's my mistake I just made the Edit - Rakesh Sivaraman
Which XSLT processor do you use? Does the code produce several result documents when you add extension-elements-prefixes="ext" to the xsl:stylesheet element? - Martin Honnen

1 Answers

1
votes

You're not too far off.

  1. It looks like you only want the text of a page element instead of all of it's children (i.e. exclude page nodes)? So you should select ./text() instead of . in your value-of.

  2. You want to recrusively apply the same template for all page elements, so you'll want an apply-template for page elements at the end of your template.

  3. position() isn't quite what you want to get a unique number. Instead you want to count all the preceeding and ancestor page elements.

Something like this:

<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{1 + count(ancestor::*[name() = 'page'] | preceding::*[name() = 'page'])}.html">
     <html>
       <head></head>
       <body><xsl:value-of select="./text()"/></body>
     </html>
   </ext:document>
   <xsl:apply-templates select="page"/>
 </xsl:template>

</xsl:stylesheet>