[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] creating top-level elements from within child elements


Subject: Re: [xsl] creating top-level elements from within child elements
From: RQamar <qamar_rahil@xxxxxxxxxxx>
Date: Tue, 22 Feb 2005 15:15:54 +0000

If I remember correctly you had

<A1>
 <xsl:apply-templates/>
</A1>

which is an instruction to place the result tree fragment generated by
applying templates as the child of an A1 element.

if you don't want the result to be placed as a child of A1  don't put it
at that point in the stylesheet, place teh A1 element it in the
stylesheet where you want it in the result, as a sibling of A2.



David




Oh Im sorry but this has brought me back to our discussion yesterday where you said that a straight-forward way of making A1 and A2 siblings is to simply write

<A1>
 <xsl:apply-templates/>
</A1>
<A2>
 <xsl:apply-templates/>
</A2>

The problem Im facing with this approach is that I do not want A2 to appear in the output xml file unless some condition in the A1 template is true. This brings me back to the use of a 'flag' boolean variable which will check whether its value is 'true' before creating A2.

However it would be much more efficient to have some feature in XSLT which will allow you to position an element outside the template.

-------------------------------
The code below is for the benefit of Jarno and Omprakash who were confused about my requirement:

The output of my present transformation is of the form



<?xml version="1.0" encoding="UTF-8"?>

<Top>

<PrimeConcept id="10180">
<PrimeName>Car</PrimeName>
<SubConcepts>
    <SubConcept id="10298" name="Toyota"/>
</SubConcepts>
</PrimeConcept>
</Top>



However I want the output in the form



<PrimeConcept id='10180'>Car</PrimeConcept>
<SubConcepts>
<SubConcept id='10298'>Toyota</SubConcept>
</SubConcepts>



I am not sure how you can create the element <SubConcepts> from within the <PrimeConcept> template match. My present code is


<xsl:template match="/">
<Top>
<PrimeConcept><xsl:apply-templates select="Top/TopNode" mode="top"/></PrimeConcept>
</Top>
</xsl:template>




<xsl:template match="TopNode" mode="top">
<xsl:if test="@name = $prime"> <!--e.g. resolves to 'Car'>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:element name="PrimeName"><xsl:value-of select="@name"/></xsl:element><!-- e.g. Car-->
<xsl:if test="following-sibling::TopNode[@name = $constraint] | preceding-sibling::TopNode[@name=$constraint]"> <!--e.g. resolves to 'Toyota'>


<xsl:for-each select="following-sibling::TopNode[@name = $constraint] | preceding-sibling::TopNode[@name=$constraint]"> <!--e.g. resolves to 'Toyota'> <xsl:element name="SubConcepts"> <SubConcept id="{@id}" name="{@name}"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:if> </xsl:template>




So how can I create the element <SubConcepts> below <Top> and not <PrimeConcept> ?

-----------------------------------------------------------------------------------


Thanks all for responding. Rahil


Current Thread
Keywords