Adding nodes to grandparent of current
I have a question concerning adding elements to ancestor nodes of the current. In the past, i have found ways of getting around this problem, however, i am a situation that this is only method that i can think of to get the output that i need.
Here is the summary of my situation: i have a template that will search through the body of xml doc and create a paragraph everytime it finds a w:p (paragraph) element. Easy enough; the problem is shapes can appear in the middle of the paragraph and the xml output must have all of its objects (paragraphs, shapes, tables, etc.) completely seperated. They can not be flowed together. So i need to make the current node an ancestor of the current, create the shape element with it's attributes and child nodes, and then finish up the paragraph.
Here is a sample of input that i could receive:
<body>
<para>
<text> Here would be the beginning of a paragraph.</text>
<text> OH! looky here, some more text.</text>
<text> Last text before a shape.</text>
<shape type="rectangle" />
<text> Here would be the sum of rest of the text runs.</text>
</para>
<para>
<text> Here would be another paragraph.</text>
</para>
<para>
<text> And another paragraph, but no shapes. </text>
</para>
<body>
So here are the templates that i am trying to use:
<xsl:template match="/">
<new-body>
<xsl:apply-templates/>
</new-body>
</xsl:template>
<xsl:template match="para">
<new-paragraph>
<make attributes/>
<xsl:for-each select="child::*">
<xsl:if test="text">
<text-run><xsl:value-of select="text"/></text-run>
</xsl:if>
<xsl:if test="shape">
<xsl:element name="new-paragraph">
<new-shape>
<xsl:value-of select="shape/@type"/>
</new-shape>
<xsl:element name="new-paragraph">
<give new new-paragraph same attributes/>
</xsl:if>
</xsl:for-each>
</new-paragraph>
</xsl:template>
This would be the type of format that i need to be the output.
<new-body>
<new-paragraph>
<text-run/>
<text-run/>
<text-run/>
<text-run/>
</new-paragraph>
<new-shape/>
<new-paragraph>
<text-run/>
<text-run/>
<text-run/>
<text-run/>
</new-paragraph>
<new-paragraph>
<text-run/>
<text-run/>
<text-run/>
<text-run/>
</new-paragraph>
</new-body>
As you can see the shape is outside of the paragraph. It probably is wiser to use templates instead of for-each and if statements, but i don't see anyway around this problem.
|