xsl-list
[Top] [All Lists]

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

2010-01-18 03:26:32

A stylesheet can navigate freely within the source document, processing
nodes in any order it likes; but the result document is in effect written
sequentially. If you want to output two elements X and Y in that order, then
the code to output X has to come in your stylesheet before the code to
output Y.

Another way of saying this is that if the input and output structures don't
match, the stylesheet structure has to match the output structure, not the
input structure. This becomes second nature once you have the processing
model clearly embedded in your subconscious, but it's not always obvious to
beginners.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

 

-----Original Message-----
From: cvergara(_at_)zedat(_dot_)fu-berlin(_dot_)de 
[mailto:cvergara(_at_)zgaedat(_dot_)fu-berlin(_dot_)de] 
Sent: 18 January 2010 02:55
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Creating elements in an arbitrary position 
in the result document

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>




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--