Re: [xsl] Testing condition on external XML

Subject: Re: [xsl] Testing condition on external XML
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 04 Aug 2009 18:52:45 -0700
You are very close to a solution.

Thank you for supplying working documents so as
to quickly illustrate the repair.

At 2009-08-04 21:25 -0400, Stanislav Peja wrote:
I am transforming data from a database into an
XML format. I want to run the transformation
only on a subset of all records I have
available. I put identifiers for those records
that I want to transform in a simple external
XML file, which I would like to use as filter.

Only those records should be transformed where
document(acquisition-nums.xml)//acqnum matches
tuple/atom[@name='ObjAccessionNo'].

You coded it that way and it did not work. Consider the following description of your requirement:

  "Only those records should be transformed where document(acquisition-
   nums.xml)//acqnum matches each tuple's atom[@name='ObjAccessionNo']."

This has a slightly different spin that solves the problem.

The way I constructed the test does not work and
the whole set from the source file is transformed.

Because you are processing every tuple when any tuple matches the criteria.


I got stuck on this and can't move further. I
appreciate any hint or help on this. I am using Saxon B 9.1.0.7.

Thanks in advance, Standa

Here are the file samples:

File 1 - Source File (records.xml)
...
File 2 External File (acquisition-nums.xml)
...
Stylesheet
...
      <xsl:if
test="tuple/atom[@name='ObjAccessionNo']=$acq-num/items/acqnum">
      <xsl:apply-templates select="tuple"/>
      </xsl:if>

This is what I first changed that to:


      <xsl:for-each select="tuple">
        <xsl:if test="atom[@name='ObjAccessionNo']=$acq-num/items/acqnum">
          <xsl:apply-templates select="."/>
        </xsl:if>
      </xsl:for-each>

... and then made that more succinct by saying:

    <collection>
      <xsl:apply-templates select="tuple[atom[@name='ObjAccessionNo']=
                                         $acq-num/items/acqnum]"/>
    </collection>

I hope this helps.

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

t:\ftemp>type records.xml
<table name="ecatalogue">
  <tuple>
    <atom name="ObjAccessionNo">2009.030</atom>
    <atom name="ObjTitle">OBJECT TTTLE</atom>
    <atom name="DatDateCreated">2009</atom>
  </tuple>
<tuple>
    <atom name="ObjAccessionNo">2009.031</atom>
    <atom name="ObjTitle">OBJECT TTTLE</atom>
    <atom name="DatDateCreated">2009</atom>
  </tuple>
</table>

t:\ftemp>type acquisition-nums.xml

<items>
  <acqnum>2004.045</acqnum>
  <acqnum>2005.098</acqnum>
  <acqnum>2008.007</acqnum>
  <acqnum>2009.030</acqnum>
</items>

t:\ftemp>call xslt2 records.xml stanislav.xsl
<?xml version="1.0"
encoding="UTF-8"?><collection><record><identifier>2009.030</identifier><title
OBJECT
TTTLE</title></record></collection>
t:\ftemp>type stanislav.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" name="xml"/>
  <xsl:variable name="acq-num" select="document('acquisition-nums.xml')"/>

  <xsl:template match="table[@name='ecatalogue']">
    <collection>
      <xsl:apply-templates select="tuple[atom[@name='ObjAccessionNo']=
                                         $acq-num/items/acqnum]"/>
    </collection>
    </xsl:template>

  <xsl:template match="tuple">
     <record>
       <identifier>
          <xsl:value-of select="atom[@name='ObjAccessionNo']"/>
        </identifier>
      <title>
        <xsl:value-of select="atom[@name='ObjTitle']"/>
       </title>
        </record>
    </xsl:template>

</xsl:stylesheet>

t:\ftemp>rem Done!



--
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