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

RE: RE: result document flush

Subject: RE: RE: result document flush
From: Robby Pelssers <Robby.Pelssers@xxxxxxx>
Date: Wed, 18 Jul 2012 15:43:14 +0200
RE:  RE: result document flush
Hi all,

The issue has been resolved (at least I now know how to get my more complex
use case working).

I created a simplified version to spin my head around the issue of splitting
several XML files into separate files. The issue I ran into was having
duplicate URI's to write to.  It would be satisfying to e.g. skip duplicate
entries from being processed so here are my inputs and a working solution. Any
improvements are welcome of course ;-)

The manifest.xml is the input for the XSLT by the way.
-----------------------manifest.xml-----------------------

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <file href="basictypes.xml"/>
  <file href="packages.xml"/>
</manifest>

-----------------------basictypes.xml-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<basictypes>
    <basictype identifier="PH3330L">
        <description>N-channel TrenchMOS logic level FET</description>
        <magcode>R73</magcode>
    </basictype>
    <basictype identifier="BUK3F00-50WDFE">
        <description>9675 AUTO IC (IMPULSE)</description>
        <magcode>R73</magcode>
    </basictype>
    <basictype identifier="PH3330L">
        <description>duplicate entry for PH33330L</description>
        <magcode>R73</magcode>
    </basictype>
</basictypes>

-----------------------packages.xml-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<packages>
    <package id="SOT669">
        <description>plastic single-ended surface-mounted package; 4
leads</description>
        <name>LFPAK; Power-SO8</name>
    </package>
    <package id="SOT600-1">
        <description>plastic thin fine-pitch ball grid array
package;</description>
        <name>TFBGA208</name>
    </package>
</packages>

-----------------------manifest_transformer.xsl-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:pelssers="http://robbypelssers.blogspot.com"
  version="2.0">

  <xsl:param name="sourceFolder"
select="xs:anyURI('file:///c:/pelssers/demo/')"/>
  <xsl:param name="destinationFolder"
select="xs:anyURI('file:///c:/pelssers/demo/export/')"/>

  <xsl:function name="pelssers:getURI" as="xs:anyURI">
    <xsl:param name="element" as="element()"/>
    <xsl:apply-templates select="$element" mode="getURI"/>
  </xsl:function>

  <xsl:template match="/">
   <xsl:variable name="elements" select="for $doc in (for $href in
manifest/file/@href return document(xs:anyURI(concat($sourceFolder, $href)))
) return $doc/*/*"/>
   <xsl:for-each-group select="$elements" group-by="pelssers:getURI(.)">
     <xsl:apply-templates select="current-group()[1]" mode="write"/>
     <xsl:apply-templates select="subsequence(current-group(), 2)"
mode="skip"/>
   </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="basictype | package" mode="write">
    <xsl:variable name="uri" select="pelssers:getURI(.)"/>
    <xsl:message>Processing <xsl:value-of select="local-name()"/> to URI
<xsl:value-of select="$uri"/> </xsl:message>
    <xsl:result-document method="xml" href="{$uri}">
      <xsl:element name="{../local-name()}">
        <xsl:apply-templates select="../@*"/>
        <xsl:copy-of select="."/>
      </xsl:element>
    </xsl:result-document>
  </xsl:template>

  <xsl:template match="basictype | package" mode="skip">
    <xsl:variable name="uri" select="pelssers:getURI(.)"/>
    <xsl:message>Warning !! Skipping duplicate <xsl:value-of
select="local-name()"/> with URI <xsl:value-of select="$uri"/> </xsl:message>
  </xsl:template>

  <xsl:template match="basictype" as="xs:anyURI" mode="getURI">
    <xsl:sequence select="xs:anyURI(concat($destinationFolder, 'basictypes/',
@identifier, '.xml'))"/>
  </xsl:template>

  <xsl:template match="package" as="xs:anyURI" mode="getURI">
    <xsl:sequence select="xs:anyURI(concat($destinationFolder, 'packages/',
@id, '.xml'))"/>
  </xsl:template>

</xsl:stylesheet>
----------------------------------------------
Message output from XSLT
[Saxon-HE] Processing basictype to URI
file:///c:/pelssers/demo/export/basictypes/PH3330L.xml
[Saxon-HE] Warning !! Skipping duplicate basictype with URI
file:///c:/pelssers/demo/export/basictypes/PH3330L.xml
[Saxon-HE] Processing basictype to URI
file:///c:/pelssers/demo/export/basictypes/BUK3F00-50WDFE.xml
[Saxon-HE] Processing package to URI
file:///c:/pelssers/demo/export/packages/SOT669.xml
[Saxon-HE] Processing package to URI
file:///c:/pelssers/demo/export/packages/SOT600-1.xml

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.