0

I have a complete docbook xml file there are nested <section> elements, I want to split xml base on the <section> tag, Could you please support to sort out this concern for your reference here with attached input sample and required output. Thanks in advance

Input File:

<?xml version="1.0" encoding="UTF-8"?>
<section id="audit">
    <sectioninfo>
        <titleabbrev>Abbrev title here</titleabbrev>
        <title>I am title</title>
    </sectioninfo>
    <para>Content here</para>
    <para>content here</para>
    
    <section id="introduction">
        <sectioninfo>
            <titleabbrev>Introduction</titleabbrev>
            <title>Introduction</title>
        </sectioninfo>
        
        <section id="aag_1">
            <sectioninfo>
                <titleabbrev>1.01</titleabbrev>
                <title>1.01</title>
            </sectioninfo>
            <para>Content here</para>
        </section>
        
        <section id="aag_2">
            <sectioninfo>
                <titleabbrev>1.02</titleabbrev>
                <title>1.02</title>
            </sectioninfo>
            <para>Content here</para>
        </section>
    </section>
</section>

Required Output File:

section_audit.xml

<section id="audit">
    <sectioninfo>
        <titleabbrev>Abbrev title here</titleabbrev>
        <title>I am title</title>
    </sectioninfo>
    <para>Content here</para>
    <para>content here</para>
</section>

section_introduction.xml

<section id="introduction">
    <sectioninfo>
        <titleabbrev>Introduction</titleabbrev>
        <title>Introduction</title>
    </sectioninfo>
</section>

section_aag_1.xml

<section id="aag_1">
    <sectioninfo>
        <titleabbrev>1.01</titleabbrev>
        <title>1.01</title>
    </sectioninfo>
    <para>Content here</para>
</section>

section_aag_2.xml

<section id="aag_2">
    <sectioninfo>
        <titleabbrev>1.02</titleabbrev>
        <title>1.02</title>
    </sectioninfo>
    <para>Content here</para>
</section>

XSL File:

<xsl:for-each select="chapter/section">
    <xsl:variable name="output" select="concat(@id, '.xml')"/>
    <xsl:result-document href="{concat('section_', $output)}" method="xml" encoding="utf-8">         
        <book>
            <xsl:if test="@id">
                <xsl:attribute name="id" select="@id"/>
            </xsl:if>
            <xsl:copy-of select="ancestor::book/bookinfo" copy-namespaces="no"/>
            <section>
                <xsl:if test="@id">
                    <xsl:attribute name="id" select="@id"/>
                </xsl:if>
                <xsl:copy-of select="." copy-namespaces="no"/>
            </section>
        </book>
    </xsl:result-document>
</xsl:for-each>

1 Answer 1

1

I would use recursion e.g.

  <xsl:template match="section[@id]">
    <xsl:result-document href="section_{@id}.xml">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:result-document>
  </xsl:template>

or even simpler

  <xsl:template match="section[@id]">
    <xsl:result-document href="section_{@id}.xml">
      <xsl:next-match/>
    </xsl:result-document>
  </xsl:template>

(plus the identity transformation template e.g declared in XSLT 3 with xsl:mode or spelled out in XSLT 2).

Sign up to request clarification or add additional context in comments.

4 Comments

Like that I want output:
<section id="introduction"> <sectioninfo> <titleabbrev>Introduction</titleabbrev> <title>Introduction</title> </sectioninfo> </section>
Thanks, Martin: How to apply the section child element but not nested section element
The suggested recursive approach will simply produce a new result document for a nested section, like you wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.