XML/XSLT stylesheet/result document

Материал из Web эксперт
Перейти к: навигация, поиск

result-document demo

File: Data.xml
<?xml version="1.0"?>
<book>
  <title>XSLT Topics</title>
  <chapter>
    <title>XPath</title>
    <para>text</para>
  </chapter>
</book>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
  extension-element-prefixes="redirect">
  <xsl:output method="html"/>
  <xsl:template match="/">
          <xsl:result-document method="html" 
            include-content-type="no"
            href="{concat("chapter", position(), ".html")}">
            <xsl:apply-templates select="." mode="create-html-file"/>
            <xsl:fallback>
              <redirect:write 
                select="concat("chapter", position(), ".html")">
                <xsl:apply-templates select="." mode="create-html-file"/>
                <xsl:fallback>
                  <a name="{concat("chapter", position())}"/>
                  <h1>
                    <xsl:value-of select="title"/>
                  </h1>
                  <xsl:apply-templates select="*[position() > 1]"/>
                </xsl:fallback>
              </redirect:write>
            </xsl:fallback>
          </xsl:result-document>
  </xsl:template>
  <xsl:template match="chapter" mode="separate-files">
    <li>
      <a href="{concat("chapter", position(), ".html")}">
        <xsl:value-of select="title"/>
      </a>
    </li>
  </xsl:template>
  <xsl:template match="chapter" mode="create-html-file">
    <html>
      <head>
        <title><xsl:value-of select="title"/></title>
      </head>
      <body style="font-family: sans-serif;">
        <h1><xsl:value-of select="title"/></h1>
        <xsl:apply-templates select="*[position() > 1]"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>