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: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 17 Jan 2010 22:05:06 -0500
At 2010-01-18 03:55 +0100, cvergara@xxxxxxxxxxxxxxxxxx wrote:
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.

Yes, please forgive me for missing that copying of attributes ... I thought you were injecting them arbitrarily and did not read ahead.


I will find that information in the subtree of B, and B can be
anywhere in the source document.

This can be done.


I tried this but it didn't work:

<xsl:template match="B">
  <xsl:element name="/A/new_element">

You are missing some basic understanding of tree building ... an element is made at the point in time you are constructing the result. You have to have created the result <A> element before creating the <new_element> ... and in your earlier post you wanted it created as the first child of <A>, not as the last child before <B>.


The example below shows how you can add the element and its attributes by copying the attributes from C and D.

If you want to suppress the C and the D then you'll have to add empty templates for them.

When writing XSLT don't give up so soon! It is an extremely powerful and expressive language to get the results you need.

I hope this helps.

. . . . . . . . . . . Ken

t:\ftemp>type cristobal.xml
<A>
... &lt;- unknown/variable amount of nodes
  <B>
    <C attrC="value1"/>
    <D attrD="value2"/>
  </B>
</A>

t:\ftemp>xslt cristobal.xml cristobal.xsl
<?xml version="1.0" encoding="utf-8"?><A><new_element attrC="value1" attrD="value2"/>
... &lt;- unknown/variable amount of nodes
<B>
<C attrC="value1"/>
<D attrD="value2"/>
</B>
</A>
t:\ftemp>type cristobal.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">


<xsl:template match="A">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <new_element>
      <xsl:copy-of select="//B/C/@*"/>
      <xsl:copy-of select="//B/D/@*"/>
    </new_element>
    <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>

t:\ftemp>


-- UBL and Code List training: Copenhagen, Denmark 2010-02-08/10 XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19 XSLT/XQuery/XPath training: San Carlos, California 2010-04-26/30 Vote for your XML training: http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread