0

Hi,
How can i convert this xml file into target xml using xslt. I tried everything, but i am not able to put the subquestions inside the corresponding questions. Please help even the logic.


INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<tables>
    <table>
        <tr>
            <tc>
                <data>
                    This is the title of the table.
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    1. Question 
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    2. Question
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion1)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion2)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    3. Question
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion1)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    4. Question
                </data>
            </tc>
        </tr>
    </table>
    <table>
        <tr>
            <tc>
                <data>
                    This is the title of the table.
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    1. Question 
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    2. Question
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    3. Question
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    (Subquestion)
                </data>
            </tc>
        </tr>
        <tr>
            <tc>
                <data>
                    4. Question
                </data>
            </tc>
        </tr>
    </table>
</tables>

Target XML:

<?xml version="1.0" encoding="UTF-8"?>
<Sections>
    <section title="This is the title of the table.">
        <Questions>
            <Question>
                1.Question
            </Question>
            <Question>
                2.Question
                <SubQuestion>
                    Subquestion1
                </SubQuestion>
                <SubQuestion>
                    Subquestion2
                </SubQuestion>
            </Question>
            <Question>
                3.Question
                <SubQuestion>
                    Subquestion1
                </SubQuestion>
            </Question>
            <Question>
                4.Question
            </Question>
        </Questions>
    </section>
    <section title="This is the title of the table.">
        <Questions>
            <Question>
                1.Question
                <SubQuestion>
                    Subquestion1
                </SubQuestion>
                <SubQuestion>
                    Subquestion2
                </SubQuestion>
            </Question>
            <Question>
                2.Question
            </Question>
            <Question>
                3.Question
                <SubQuestion>
                    Subquestion
                </SubQuestion>
            </Question>
            <Question>
                4.Question
            </Question>
        </Questions>
    </section>
</Sections>

Edited by sudheer2250: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by xml_looser
0
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:key 
        name="k1"
        match="table/tr[starts-with(normalize-space(tc/data), '(')]"
        use="generate-id(preceding-sibling::tr[not(starts-with(normalize-space(), '('))][1])"/>
    
    <xsl:template match="tables">
        <Sections>
            <xsl:apply-templates/>
        </Sections>
    </xsl:template>
    
    <xsl:template match="table">
        <section title="{normalize-space(tr[1]/tc/data)}">
            <Questions>
                <xsl:apply-templates select="tr[position() &gt; 1][not(starts-with(normalize-space(), '('))]"/>
            </Questions>
        </section>
    </xsl:template>
    
    <xsl:template match="tr">
        <Question>
            <xsl:value-of select="normalize-space(tc/data)"/>
            <xsl:apply-templates select="key('k1', generate-id())" mode="sq"/>
        </Question>
    </xsl:template>
    
    <xsl:template match="tr" mode="sq">
        <SubQuestion>
            <xsl:value-of select="translate(normalize-space(tc/data), '()', '')"/>
        </SubQuestion>
    </xsl:template>
    
</xsl:stylesheet>

is not possible

I got this solution from other friend but i did not understand compeltely.

0

your friend is using the clip of subquestion
and a key set for the following nodes

your data are used in word excel table

You must your data in the xml file to examine better

there maybe some differences you fall on the can evaluate the

But differences on content is bad

For example, changing the values ​​but in
(Subquestion1) in hello

already breaks down the xsl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.