xsl-list
[Top] [All Lists]

Re: [xsl] Pairing elements according to attributes with XSLT 1

2009-07-22 04:37:25

$ cat doit.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" />

  <xsl:template match="/">
    <xsl:call-template name="doit">
      <xsl:with-param name="from" select="'big'" />
      <xsl:with-param name="to" select="'small'" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="doit">
    <xsl:param name="from" />
    <xsl:param name="to" />
    <MainWordInput xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                   xsi:noNamespaceSchemaLocation="entriesResult.xsd"
                   fromSize="{$from}" toSize="{$to}">
      <xsl:for-each select="/KeywordsInfo/KeywordsEntry">
        <xsl:variable name="kentry" select="." />
        <xsl:for-each select="keyword[(_at_)size=$from]">
          <xsl:variable name="fromkw" select="." />>
          <xsl:for-each select="$kentry/keyword[(_at_)size=$to]">
            <Entries>
              <first><xsl:value-of select="$fromkw" /></first>
              <second><xsl:value-of select="." /></second>
            </Entries>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </MainWordInput>>
  </xsl:template>

</xsl:stylesheet>

$ xsltproc doit.xsl k.xml | tidy -q -xml
<?xml version="1.0"?>.
<MainWordInput
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="entriesResult.xsd" fromSize="big"
toSize="small">
  <Entries>
    <first>bowling ball</first>
    <second>tennis ball</second>
  </Entries>
  <Entries>>
    <first>bowling ball</first>>
    <second>table tennis ball</second>
  </Entries>
  <Entries>>
    <first>mountain 1</first>
    <second>chair 1</second>>
  </Entries>
  <Entries>>
    <first>mountain 1</first>
    <second>chair 2</second>>
  </Entries>
  <Entries>>
    <first>mountain 2</first>
    <second>chair 1</second>>
  </Entries>
  <Entries>>
    <first>mountain 2</first>
    <second>chair 2</second>>
  </Entries>
</MainWordInput>

$



Mit besten Grüßen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschäftsführung: Erich Baier
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


                                                                       
             Andy Kohn                                                 
             <andydev(_at_)gmail(_dot_)co                                       
  
             m>                                                         To
                                       
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
             07/22/2009 08:52                                           cc
             AM                                                        
                                                                   Subject
                                       [xsl] Pairing elements according to
             Please respond to         attributes with XSLT 1          
             xsl-list(_at_)lists(_dot_)mu                                       
  
              lberrytech.com                                           
                                                                       
                                                                       
                                                                       
                                                                       




Hi all,

I'm trying to pair elements according to attributes using XSLT 1.

Giving an example would be easier to explain ;)

This is an example of the input XML:


<KeywordsInfo>
    <KeywordsEntry>
        <keyword size="big">bowling ball</keyword>
        <keyword size="medium">should not be included</keyword>
        <keyword size="small">tennis ball</keyword>
        <keyword size="small">table tennis ball</keyword>
    </KeywordsEntry>
    <KeywordsEntry>
        <keyword size="big">mountain 1</keyword>
        <keyword size="big">mountain 2</keyword>
        <keyword size="small">chair 1</keyword>
        <keyword size="small">chair 2</keyword>
    </KeywordsEntry>
    <KeywordsEntry>
        <keyword size="medium">should not be excluded at all</keyword>
        <keyword size="tiny">should not be excluded at all</keyword>
    </KeywordsEntry>
</KeywordsInfo>


I need to create an XSLT 1, that gets two parameters, fromSize and
toSize, which then creates pairs of this entries.

For example, if I send fromSize=big, toSize=small, I need to get this
result:


<MainWordInput xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
            xsi:noNamespaceSchemaLocation="entriesResult.xsd"
            fromSize ="big"
            toSize="small">
   <Entries>
      <first>bowling ball</first>
      <second>tennis ball</second>
   </Entries>
   <Entries>
      <first>bowling ball</first>
      <second>table tennis ball</second>
   </Entries>
   <Entries>
      <first>mountain 1</first>
      <second>chair 1</second>
   </Entries>
   <Entries>
      <first>mountain 1</first>
      <second>chair 2</second>
   </Entries>
   <Entries>
      <first>mountain 2</first>
      <second>chair 1</second>
   </Entries>
   <Entries>
      <first>mountain 2</first>
      <second>chair 2</second>
   </Entries>
</MainWordInput>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--