Question

I am new to XSL and I am trying to flatten an XML file with the following structure with the objective of using it in InDesign (the real XML structure is a lot more complex and actually follows the NLM schema but the example below should work to illustrate what I need):

Ex.

 <section>
    <p> just normal text here 1</p>
    <section>
        <p>just normal text for this section</p>
    </section>
    <p>just normal text here 2</p>
    <section>
        <p>just more text</p>
        <section>
            <p>this is the text in the deepest section </p>
        </section>
        <p>and even more text </p>
    </section>
        <p>just normal text here 3</p>
  </section>

I have almost accomplished what I needed with the following XSLT:

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

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


<xsl:template match="section">
   <xsl:variable name="sectionlevel">
      <xsl:value-of select="count(ancestor::section)" />
   </xsl:variable>
   <xsl:element name="s{$sectionlevel}">  

       <xsl:apply-templates select="descendant::section" />
       <xsl:copy-of select="*[local-name() != 'section']"/>

   </xsl:element>
</xsl:template>             
</xsl:stylesheet>

The output I get with this code is the one shown below which would be fine but the problem is that I need to keep the order of the elements. I only need to change the section element names and keep everything else the same:

<?xml version="1.0" encoding="utf-8"?>
<s0>
  <s1>
    <p> just normal text for this section</p>
  </s1>
  <s1>
    <s2>
      <p>this is the text in the deepest section </p>
    </s2>
    <p>just more text</p>
    <p>and even more text </p>
  </s1>
  <s2>
    <p>this is the text in the deepest section </p>
  </s2>
  <p> just normal text here 1</p>
  <p>just normal text here 2</p>
  <p>just normal text here 3</p>
</s0>

As you can see the

elements in this example are moved to the end inside the section element. How should I code the XSL transformation so that if keeps all the original XML order and structure and just changes the section labels?

Was it helpful?

Solution

Is this what you expect?

<?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"/>

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

    <xsl:template match="section" >
        <xsl:element name="s{count(ancestor::section)}">
            <xsl:apply-templates select="@*|node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This would output:

<?xml version="1.0" encoding="UTF-8"?>
<s0>
    <p> just normal text here 1</p>
    <s1>
        <p>just normal text for this section</p>
    </s1>
    <p>just normal text here 2</p>
    <s1>
        <p>just more text</p>
        <s2>
            <p>this is the text in the deepest section </p>
        </s2>
        <p>and even more text </p>
    </s1>
    <p>just normal text here 3</p>
</s0>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top