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>

Dani AI

Generated

The transformation is doable. The right strategy is grouping rows into a title, questions, and the question’s following subquestion rows — the friend’s approach on the thread follows exactly that idea, so was on the right track and ’s skepticism is not necessary.

High-level logic (no copy of the posted XSLT): treat the first <tr> as the section title, then scan the remaining <tr> elements in document order. Classify a row as a question when its normalized text does not begin with an opening parenthesis, and as a subquestion when it does. Use a grouping technique so each subquestion is attached to the nearest preceding question. In XSLT 1.0 that grouping is commonly implemented with a key: index subquestion rows by the generate-id of the question row that immediately precedes them, then when outputting a Question element use that generate-id to retrieve its SubQuestion rows. Use normalize-space to trim newlines/extra spaces and translate to strip the parentheses when emitting subquestion text.

Practical robustness tips: trim and normalize every data node before testing (Word/Excel exports often introduce CR, tabs, or non-breaking spaces — treat U+00A0 as whitespace). Don’t rely on visual indentation; test the first character with substring(normalize-space(...),1,1). If you can use XSLT 2.0, group-starting-with makes the same job much simpler: group question rows and their following subrows, then emit the group’s first item as the Question and the rest as SubQuestion elements.

Debugging hints: output a simple list of normalized row values and their computed “kind” (question/subquestion) to verify your tests; confirm the input really uses <tr>/<tc>/<data> as shown and that there are no invisible characters breaking starts-with checks.

Recommended Answers

All 3 Replies

is not possible

<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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.