Question

I am trying to make an XSLT transformation from XML, I want to transform book content, I have XML file like this and I need to transform it with XSLT. I know hot to create templates for every section but I have only basic experiences with XSLT, so my I do not know how to apply it.

<book>
    <section id="1">
        <name>Heading First chapter</name>
        <p><i/> some text </p>
        <p> other text </p>
        <subsection id="1.1">
            <name>Heading second chapter</name>
                <subsubsection id="1.1.1">
                    <name>Heading third chapter</name>
                    <p>some text</p>
                </subsubsection>
        </subsection>
    </section>
</book>

What I want is HTML like this:

<div>
    <h1>1 Heading First chapter</h1>
    <p><i>some text</i>
    <p>other text</p>
    <div>
        <h2>1.1 Heading second chapter</h2>
        <div>
            <h3>1.1.1 Heading third chapter</h3>
            <p>some text</p>        
        </div>
    </div>
</div>

My try:

<xsl:template match="/">
         <body>
        <xsl:for-each select="book">
          <xsl:apply-templates select="."></xsl:apply-templates>
        </xsl:for-each>    
      </body>
    </html>
  </xsl:template>

<xsl:template match="p">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

<xsl:template match="p[i]">
    <p>
      <em>
        <xsl:value-of select="."/>
      </em>
    </p>
  </xsl:template>

<xsl:template match="section/name">
    <p>
      <h2>            
          <xsl:value-of select="."/>
        </a>
      </h2>
    </p>
  </xsl:template>

Do you know how to make a transformation ?

Was it helpful?

Solution

When I apply the next XSLT:

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

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

    <xsl:template match="book">
        <body>
            <xsl:apply-templates select="node()" />
        </body>
    </xsl:template>

    <xsl:template match="*[contains(local-name(), 'section')]">
        <div>
            <xsl:apply-templates select="node()" />
        </div>
    </xsl:template>

    <xsl:template match="name">
        <xsl:element name="h{count(ancestor::*[contains(local-name(), 'section')])}">
            <xsl:value-of select="concat(parent::*/@id, ' ', .)" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

To the provided input XML

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <section id="1">
        <name>Heading First chapter</name>
        <p>
            <i/> some text </p>
        <p> other text </p>
        <subsection id="1.1">
            <name>Heading second chapter</name>
            <subsubsection id="1.1.1">
                <name>Heading third chapter</name>
                <p>some text</p>
            </subsubsection>
        </subsection>
    </section>
</book>

It produces:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <div>
        <h1>1 Heading First chapter</h1>
        <p>
            <i/> some text </p>
        <p> other text </p>
        <div>
            <h2>1.1 Heading second chapter</h2>
            <div>
                <h3>1.1.1 Heading third chapter</h3>
                <p>some text</p>
            </div>
        </div>
    </div>
</body>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top