RE: [xsl] RE: result document flush

Subject: RE: [xsl] RE: result document flush
From: Robby Pelssers <Robby.Pelssers@xxxxxxx>
Date: Wed, 18 Jul 2012 15:43:14 +0200
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