vinod Kumar
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">AA AA.</p>
<p outputclass="Normal"><b>Q: </b> Why?</p>
<p outputclass="Body_Text_Que stion"><b> Q:</b> What?</p>
<p outputclass="Body_Text_Ans wer"><b>A: </b> In.</p>
<p outputclass="Body_Text_Ans wer">The</ p>
<p outputclass="Normal"><b>Q: </b> Why?</p>
<p outputclass="Body_Text_Que stion"><b> Q:</b> What?</p>
<p outputclass="Body_Text_Ans wer"><b>A: </b> In.</p>
<p outputclass="Body_Text_Ans wer">The</ p>
</Body>
XSL I used:
<xsl:template match="p[@outputclass='Bod y_Text']">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="p[@outputclass='Nor mal']">
<item num="{replace(node()[1], '^\s+', '')}">
<para><xsl:apply-templates select="node()[position() gt 1]"/></para>
</item>
</xsl:template>
<xsl:template match="p[@outputclass='Bod y_Text_Que stion'][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='Bod y_Text_Ans wer'][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='Bod y_Text_Ans wer']">
<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
XML I used as:
<Body>
<p outputclass="Body_Text">AA
<p outputclass="Normal"><b>Q:
<p outputclass="Body_Text_Que
<p outputclass="Body_Text_Ans
<p outputclass="Body_Text_Ans
<p outputclass="Normal"><b>Q:
<p outputclass="Body_Text_Que
<p outputclass="Body_Text_Ans
<p outputclass="Body_Text_Ans
</Body>
XSL I used:
<xsl:template match="p[@outputclass='Bod
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="p[@outputclass='Nor
<item num="{replace(node()[1], '^\s+', '')}">
<para><xsl:apply-templates
</item>
</xsl:template>
<xsl:template match="p[@outputclass='Bod
<item num="{replace(node()[1], '^\s+', '')}">
<para><xsl:apply-templates
</item>
</xsl:template>
<xsl:template match="p[@outputclass='Bod
<item num="{replace(node()[1], '^\s+', '')}">
<para><xsl:apply-templates
</item>
</xsl:template>
<xsl:template match="p[@outputclass='Bod
<para><xsl:apply-templates
</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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
<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)
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
The follow up question has been directed to a new question so this case is closed
You can use the technique I passed on in that solution.
Please close questions first before shooting new similar questions