Re: [xsl] Creating elements in an arbitrary position in the result document

Subject: Re: [xsl] Creating elements in an arbitrary position in the result document
From: cvergara@xxxxxxxxxxxxxxxxxx
Date: Mon, 18 Jan 2010 03:55:21 +0100
Thank you very much for your answer Ken.
Your code will not solve my problem, because in the template that matches
A I don't know the attribute names and values for attrC, attrD, value1,
and value2. I will find that information in the subtree of B, and B can be
anywhere in the source document. So, I guess the only solution would be to
match B and then write the new nodes right after A, but I am not able to
find documentation on how to do that. But thanks anyways.

I tried this but it didn't work:

<xsl:template match="B">
  <xsl:element name="/A/new_element">
    <xsl:attribute name="attrC">
      <xsl:value-of select="C/attrC"/>
    </xsl:attribute>
    The same for D
  </xsl:element>
</xsl:template>

>>Source:
>><?xml version="1.0"?>
>><A>
>>... <- unknown/variable amount of nodes
>>   <B>
>>     <C attrC="value1"/>
>>       <D attrD="value2"/>
>>   </B>
>></A>
>>Result:
>><A>
>>   <new_element attrC="value1" attrD="value2"/>
>>     ... <- unknown amount of elements in source between A and B
>>     <B>
>>...
>
> <xsl:template match="A">
>    <xsl:copy>
>      <xsl:copy-of select="@*"/>
>      <new_element attrC="value1" attrD="value2"/>
>      <xsl:apply-templates select="node()"/>
>    </xsl:copy>
> </xsl:template>
>
> <xsl:template match="@*|node()"><!--identity for all other nodes-->
>    <xsl:copy>
>      <xsl:apply-templates select="@*|node()"/>
>    </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>

Current Thread