简体   繁体   English

使用XSLT将XML拆分为多个HTML页面?

[英]Splitting XML into multiple HTML pages with XSLT?

I have an XML document. 我有一个XML文档。 I would like to transform this single file into multiple HTML files. 我想将单个文件转换为多个HTML文件。 when I'm using ext:document inside <xsl:template match="page/page"> the code is not working what is i am doing wrong? 当我在<xsl:template match="page/page">使用ext:document ,代码无法正常工作我在做什么错?

XML XML

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

My XSLT 我的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 page1.html

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

page2.html page2.html

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

page3.html page3.html

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

page4.html page4.html

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

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 (ie exclude page nodes)? 看来您只希望页面元素的文本而不是其所有子元素(即,排除页面节点)? So you should select ./text() instead of . 因此,您应该选择./text()而不是. in your value-of . 以您的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. 您希望将相同的模板应用于所有页面元素,因此,在模板末尾需要一个页面元素的apply-template template。

  3. position() isn't quite what you want to get a unique number. position()并不是您想要获得唯一编号的方法。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM