[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Grouping and Insertion

Subject: Re: Grouping and Insertion
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Mon, 20 Nov 2006 12:00:39 +0100 (CET)
Re:  Grouping and Insertion
Jeff Sese wrote:

  Hi

> I have a source xml that needs to be inserted with new
> contents from a separate xml file.

  Personnally, I'd do something like this, if I correctly
understood your requirements:

    [11] ~/xslt/tests$ cat airplaine.xml
    <root>
        <entry id="1" type="T" n="1">
            <aaa/>
        </entry>
        <entry id="1" type="T" n="1" n-mod="a">
            <bbb/>
        </entry>
        <entry id="1" type="T" n="2">
            <ccc/>
        </entry>
        <entry id="1" type="T" n="3">
            <ddd/>
        </entry>
        <entry id="1" type="T" n="4">
            <eee/>
        </entry>
        <entry id="1" type="T" n="5">
            <fff/>
        </entry>
    </root>

    [12] ~/xslt/tests$ cat airplaine-1.xml
    <root>
        <entry id="1" type="T" n="1"/>(<xref target="1" type="T"
    n="1">1</xref>) <AAA/><entry id="1" type="T" n="1"
    n-mod="a"/>(<xref target="1" type="T" n="1a">1a</xref>)
<BBB/><entry
    id="1" type="T" n="1-5"/>(<xref target="1" type="T"
    n="1-5">1-5</xref>) <CCC/><entry id="1" type="T"
    n="2"/>(<xref target="1" type="T" n="2">2</xref>) <DDD/>
    </root>

    [13] ~/xslt/tests$ cat airplaine.xsl
    <xsl:stylesheet
        version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:my="my:airplaine.xsl"
        exclude-result-prefixes="xs my">

      <xsl:param name="new.entry.file" as="xs:anyURI"
                 select="xs:anyURI('airplaine-1.xml')"/>

      <xsl:variable name="new.entry.doc" as="document-node()"
                    select="document($new.entry.file)"/>
      <xsl:variable name="my:id-separator" as="xs:string"
                    select="'.@@.'"/>

      <xsl:function name="my:id" as="xs:string">
        <xsl:param name="id"    as="xs:string"/>
        <xsl:param name="type"  as="xs:string"/>
        <xsl:param name="n"     as="xs:string"/>
        <xsl:param name="n-mod" as="xs:string?"/>
        <xsl:sequence select="
            string-join(
                ($id, $type, $n, $n-mod),
                $my:id-separator
              )"/>
      </xsl:function>

      <xsl:key name="xref" match="xref"
               use="my:id(@target, @type, @n, @n-mod)"/>

      <xsl:function name="my:xref-from-entry">
        <xsl:param name="entry" as="element(entry)"/>
        <xsl:variable name="id" as="xs:string" select="
          $entry/my:id(@id, @type, @n, @n-mod)"/>
        <xsl:sequence select="$new.entry.doc/key('xref', $id)"/>
      </xsl:function>

      <xsl:template match="entry">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        <xsl:variable name="xref" select="
            my:xref-from-entry(.)"/>
        <xsl:if test="$xref">
          <new.entry>
            <xsl:apply-templates select="
                $xref/following-sibling::*[1]"/>
          </new.entry>
        </xsl:if>
      </xsl:template>

      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

    </xsl:stylesheet>

    [14] ~/xslt/tests$ saxon airplaine.xml airplaine.xsl
    <?xml version="1.0" encoding="UTF-8"?><root>
        <entry id="1" type="T" n="1">
            <aaa/>
        </entry><new.entry><AAA/></new.entry>
        <entry id="1" type="T" n="1" n-mod="a">
            <bbb/>
        </entry>
        <entry id="1" type="T" n="2">
            <ccc/>
        </entry><new.entry><DDD/></new.entry>
        <entry id="1" type="T" n="3">
            <ddd/>
        </entry>
        <entry id="1" type="T" n="4">
            <eee/>
        </entry>
        <entry id="1" type="T" n="5">
            <fff/>
        </entry>
    </root>

    [15] ~/xslt/tests$ 

  Regards,

--drkm





















	

	
		
___________________________________________________________________________ 
Dicouvrez une nouvelle fagon d'obtenir des riponses ` toutes vos questions ! 
Profitez des connaissances, des opinions et des expiriences des internautes sur Yahoo! Questions/Riponses 
http://fr.answers.yahoo.com

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.