Link to home
Start Free TrialLog in
Avatar of vinod Kumar
vinod KumarFlag for India

asked on

How to create root element of my input xml?

I need to create the root element of which is not available template in the input

XML I used as:

    <Body>
    <p outputclass="Body_Text">AAAA.</p>
   
    <p outputclass="Normal"><b>Q:</b> Why?</p>
    <p outputclass="Body_Text_Question"><b>Q:</b> What?</p>
    <p outputclass="Body_Text_Answer"><b>A:</b> In.</p>
    <p outputclass="Body_Text_Answer">The</p>
   
    <p outputclass="Normal"><b>Q:</b> Why?</p>
    <p outputclass="Body_Text_Question"><b>Q:</b> What?</p>
    <p outputclass="Body_Text_Answer"><b>A:</b> In.</p>
    <p outputclass="Body_Text_Answer">The</p>
    </Body>

XSL I used:

    <xsl:template match="p[@outputclass='Body_Text']">
        <para>
          <xsl:apply-templates/>
        </para>
      </xsl:template>
     
        <xsl:template match="p[@outputclass='Normal']">
        <item num="{replace(node()[1], '^\s+', '')}">
          <para><xsl:apply-templates select="node()[position() gt 1]"/></para>
        </item>
      </xsl:template>
     
      <xsl:template match="p[@outputclass='Body_Text_Question'][b]">
        <item num="{replace(node()[1], '^\s+', '')}">
          <para><xsl:apply-templates select="node()[position() gt 1]"/></para>
        </item>
      </xsl:template>
     
      <xsl:template match="p[@outputclass='Body_Text_Answer'][b]" priority="10">
        <item num="{replace(node()[1], '^\s+', '')}">
          <para><xsl:apply-templates select="node()[position() gt 1]"/></para>
        </item>
      </xsl:template>
     
        <xsl:template match="p[@outputclass='Body_Text_Answer']">
          <para><xsl:apply-templates/></para>
         </xsl:template>
     
   
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

Output I'm getting like:

    <Body>
       
       <para>AAAA.</para>
       
       
       <item num="Q:">
          <para> Why?</para>
       </item>
       
       <item num="Q:">
          <para> What?</para>
       </item>
       
       <item num="A:">
          <para> In.</para>
       </item>
       
       <para>The</para>
       
       
       <item num="Q:">
          <para> Why?</para>
       </item>
       
       <item num="Q:">
          <para> What?</para>
       </item>
       
       <item num="A:">
          <para> In.</para>
       </item>
       
       <para>The</para>
       
    </Body>

Expected output would be(needs to close the item list after the normal para and orderlist on the end of the all question and answer types):

    <Body>
       
       <para>AAAA.</para>
       
       <orderedlist type="manual">
       <item num="Q:">
          <para> Why?</para>
       </item>
       
       <item num="Q:">
          <para> What?</para>
       </item>
       
       <item num="A:">
          <para> In.</para>
       
       
       <para>The</para></item>
       
       
       <item num="Q:">
          <para> Why?</para>
       </item>
       
       <item num="Q:">
          <para> What?</para>
       </item>
       
       <item num="A:">
          <para> In.</para>
       
       
       <para>The</para></item>
       </orderedlist>
       
    </Body>

I need to create root element (order list) for the question and answers types. Please suggest me code. I'm using XSLT version 2.0. Thank you in advance
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Again, this is a grouping issue similar to two questions ago.
You can use the technique I passed on in that solution.
Please close questions first before shooting new similar questions
SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinod Kumar

ASKER

Its working fine partially. I got the output like below:

<Body>
   <para>AAAA.</para>
   <orderedlist type="manual">
      <item num="Q:">
         <para> Why?</para>
      </item>
      <item num="Q:">
         <para> What?</para>
      </item>
      <item num="A:">
         <para> In.</para>
      </item>
      <para>The</para>

      <item num="Q:">
         <para> Why?</para>
      </item>
      <item num="Q:">
         <para> What?</para>
      </item>
      <item num="A:">
         <para> In.</para>
      </item>
      <para>The</para>

   </orderedlist>
</Body>

I need to close the <item> tag after the answer <para> tag as like below:

<Body>
   <para>AAAA.</para>
   <orderedlist type="manual">
      <item num="Q:">
         <para> Why?</para>
      </item>
      <item num="Q:">
         <para> What?</para>
      </item>
      <item num="A:">
         <para> In.</para>
      <para>The</para></item>
      <item num="Q:">
         <para> Why?</para>
      </item>
      <item num="Q:">
         <para> What?</para>
      </item>
      <item num="A:">
         <para> In.</para>
      <para>The</para></item>
   </orderedlist>
</Body>

Please suggest coding for that. Almost we got the output. But little bit needs to do. Thanks Geert.
Please note that Experts Exchange is not a code generating machine.
Expert Exchange is a forum that helps developers to help themselves.
Based on the suggestions on this forum, you should try to continue your own work.

Your questions are very much tended towards: "here is the input, here is the output I want, analyse it and develop me some code"
That is not an Experts Exchange Question, that is more something for freelancer.com,
where you get a freelancer for doing your work

The code I gave you does exactly what you needed, it adds the root node,
so you should consider this question closed.
Please close it properly now

If you have an extra requirement, well make a new question
(or do some thinking, this extra step is really easy)
- add a source in a code field so it can be easily copied
- add the XSLT you have in a code field
properly describe the issue

Maybe I will make an answer in this new question, maybe not, depends on my time, today likely not
The advantage of a new and easy question is that others might answer it

But you need to start closing questions now
(and maybe read the policy pages)
Fully answers the original question and the approach is generally useful for similar questions.

The follow up question has been directed to a new question so this case is closed