xml - How to split child node into multiple sibling child nodes each with a single subnode -


i newbie @ xslt, i'm asking assistance. have number of xml documents of following representative example. documents divided <sub-doc(n)> elements, further divided <section> elements. within sections 0 or more <heading> elements, , 1 or more <paragraph> elements. goal ensure every section has @ 1 <heading> element dividing sections have more 1 heading multiple sections 1 heading in each. when done, <paragraph> elements following <heading> have go <heading> new <section>. example, note in following example first <section> of <sub-doc1> has 2 <heading> elements. need break <section> element 2 <section> elements, each own <heading> , follow-up <paragraph> elements.

<document>  <sub-doc1>   <section> <!-- section needs split -->    <heading>subdoc1 first heading text</heading>    <paragraph>a lot of text</paragraph>    <paragraph>yet more text</paragraph>    <paragraph>more text</paragraph>    ...    <heading>subdoc1 second heading text</heading>    <paragraph>even more text</paragraph>    <paragraph>some text</paragraph>    ...   </section>   <section>    <paragraph>even more text</paragraph>    ...   </section>  </sub-doc1>  <sub-doc2>   <section>    <heading>subdoc2, first heading text</heading>    <paragraph>a lot of text here</paragraph>    <paragraph>yet more text here</paragraph>    <paragraph>yet more text here</paragraph>    ...   </section>  </sub-doc2> </document>   

that is, transformed document needs this:

<document>  <sub-doc1>   <section> <!-- section got split 2 sections -->    <heading>subdoc1 first heading text</heading>    <paragraph>a lot of text</paragraph>    <paragraph>yet more text</paragraph>    <paragraph>more text</paragraph>    ...   </section>   <section> <!-- new section -->    <heading>subdoc1 second heading text</heading>    <paragraph>even more text</paragraph>    <paragraph>some text</paragraph>    ...   </section>   <section>    <paragraph>even more text</paragraph>    ...   </section>  </sub-doc1>  <sub-doc2>   <section>    <heading>subdoc2, first heading text</heading>    <paragraph>a lot of text here</paragraph>    <paragraph>yet more text here</paragraph>    <paragraph>yet more text here</paragraph>    ...   </section>  </sub-doc2> </document>   

note sections not have <heading> elements @ all. in cases, sections should remain same. also, sections have single <heading>. sections should remain same. , else in document should remain same. transformation needs occur in cases <section> anywhere in document has more 1 <heading>.

again, new xslt, , can't head around xsl accomplish task. assistance.

this stylesheet ask. uses key select paragraph elements corresponding given heading.

the output show corresponds sample input ellipses removed.

note solution requires section elements contain heading elements have heading element first child. i.e. there no paragraph elements before first heading. if assumption wrong stylesheet need small change.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:strip-space elements="*"/>   <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>    <xsl:key name="paragraph-for-heading" match="paragraph" use="generate-id(preceding-sibling::heading[1])"/>    <xsl:template match="node()">     <xsl:copy>       <xsl:apply-templates/>     </xsl:copy>   </xsl:template>    <xsl:template match="section[heading]">     <xsl:apply-templates select="heading"/>   </xsl:template>    <xsl:template match="heading">     <section>       <xsl:copy>         <xsl:apply-templates/>       </xsl:copy>       <xsl:apply-templates select="key('paragraph-for-heading', generate-id())"/>     </section>   </xsl:template>  </xsl:stylesheet> 

output

<document>    <sub-doc1>       <section>          <heading>subdoc1 first heading text</heading>          <paragraph>a lot of text</paragraph>          <paragraph>yet more text</paragraph>          <paragraph>more text</paragraph>       </section>       <section>          <heading>subdoc1 second heading text</heading>          <paragraph>even more text</paragraph>          <paragraph>some text</paragraph>       </section>       <section>          <paragraph>even more text</paragraph>       </section>    </sub-doc1>    <sub-doc2>       <section>          <heading>subdoc2, first heading text</heading>          <paragraph>a lot of text here</paragraph>          <paragraph>yet more text here</paragraph>          <paragraph>yet more text here</paragraph>       </section>    </sub-doc2> </document> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -