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

Re: Aligning/merging two sequences

Subject: Re: Aligning/merging two sequences
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 30 Sep 2010 19:38:40 +0200
Re:  Aligning/merging two sequences
Markus Flatscher wrote:

Suppose I have two inputs like so, where input1//w is always a subset of input2//w:

<input1>
 <w n="1">I</w>
 <w n="2">am</w>
 <w n="3">a</w>
 <w n="4">sequence</w>
</input1>

<input2>
 <w>I</w>
 <w>am</w>
 <w>a</w>
 <w>longer</w>
 <w>longer</w>
 <w>sequence</w>
</input2>

I'd like to get output like so:

<output>
 <w n="1">I</w>
 <w n="2">am</w>
 <w n="3">a</w>
 <w n="skipped">longer</w>
 <w n="skipped">longer</w>
 <w n="4">sequence</w>
</output>

I.e., for each input1//w, @n should be copied to the nearest following sibling <w> in input2 that matches .; <w>s in input2 that aren't in input1 should be flagged as "skipped".

I tried it like this:


<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:param name="inp1" as="xs:string" select="'input1.xml'"/>
<xsl:variable name="input1" as="element(input1)" select="document($inp1)/input1"/>


<xsl:output method="xml" indent="yes"/>

<xsl:key name="k1" match="input1/w" use="."/>

  <xsl:template match="input2">
    <output>
      <xsl:apply-templates select="w[1]">
        <xsl:with-param name="skipped" select="0"/>
      </xsl:apply-templates>
    </output>
  </xsl:template>

<xsl:template match="input2/w">
<xsl:param name="skipped" as="xs:integer"/>
<xsl:variable name="pos" as="xs:integer" select="position()"/>
<xsl:variable name="match" select="$input1/w[$pos + $skipped][. = current()]"/>
<w n="{if ($match) then $match/@n else 'skipped'}">
<xsl:value-of select="."/>
</w>
<xsl:apply-templates select="following-sibling::w[1]">
<xsl:with-param name="skipped" select="$skipped + count($match)"/>
</xsl:apply-templates>
</xsl:template>


</xsl:stylesheet>

That assumes your longer sequence as the primary input document and the shorter as input parameter.


--


	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

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.