[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Term/Definition Lookup


Subject: Re: [xsl] Term/Definition Lookup
From: "Terry Badger terry_badger@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 12 Jun 2014 00:34:10 -0000

Rick,
Try this. Since this is a lookup problem keys make it easy.
Terry
<?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:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
   version="2.0">
   <xsl:key name="lookup" match="term" use="."/>
   <xsl:variable name="root" select="/"/>
   <xsl:template match="/root">
      <xsl:result-document href="term-def-output.xml">
         <xsl:copy>
            <xsl:for-each select="tokenize(terms , ',')">
               <xsl:value-of select="key('lookup' , normalize-space(.) ,
$root)/following-sibling::def[1]"/>
               <xsl:if test="position() != last()">
                  <xsl:text>, </xsl:text>
               </xsl:if>
            </xsl:for-each>
         </xsl:copy>
      </xsl:result-document>
   </xsl:template>
</xsl:stylesheet>


On Wednesday, June 11, 2014 3:36 PM, "Ihe Onwuka ihe.onwuka@xxxxxxxxx"
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:




<xsl:template match="root">
  <root>
     <defs>
       <xsl:apply-templates select="lookup/def"/>
     </defs>
  </root>
</xsl:template>
  
<xsl:template match="def[following-sibling::def]"> 
   <xsl:apply-templates/>
   <xsl:text>,</xsl:text>  
</xsl:template>




On Wed, Jun 11, 2014 at 8:01 PM, Rick Quatro rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,
>
>I have a comma-separated list of "terms". I want to loop through each term
>and end up with a comma-separated list of definitions. I am using XSLT 1.0.
>Here is my xml:
>
><?xml version="1.0" encoding="UTF-8"?>
><root>
>    <terms>A, B, C, D, E</terms>
>    <lookup>
>        <term>A</term>
>        <def>Def for A</def>
>        <term>B</term>
>        <def>Def for B</def>
>        <term>C</term>
>        <def>Def for C</def>
>        <term>D</term>
>        <def>Def for D</def>
>        <term>E</term>
>        <def>Def for E</def>
>    </lookup>
></root>
>
>The xml file has a built-in "lookup table" and here is the desired output:
>
><?xml version="1.0" encoding="UTF-8"?>
><root>
>   <defs>Def for A, Def for B, Def for C, Def for D, Def for E</defs>
></root>
>
>Here is my stylesheet:
>
><?xml version="1.0" encoding="UTF-8"?>
><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    version="1.0">
>
>    <xsl:output method="xml" indent="yes"/>
>
>    <xsl:template match="/">
>        <xsl:apply-templates/>
>    </xsl:template>
>
>    <xsl:template match="lookup"/>
>
>    <xsl:template match="terms">
>        <root>
>            <defs>
>                <xsl:call-template name="get-defs">
>                    <xsl:with-param name="list" select="."/>
>                </xsl:call-template>
>            </defs>
>        </root>
>    </xsl:template>
>
>    <xsl:template name="get-defs">
>        <xsl:param name="list"/>
>        <xsl:variable name="wlist"
>select="concat(normalize-space(translate($list,',',' ')),' ')"/>
>        <xsl:choose>
>            <xsl:when test="$wlist!=' '">
>                <xsl:variable name="first" select="substring-before($wlist,'
>')"/>
>                <xsl:variable name="rest" select="substring-after($wlist,'
>')"/>
>                <xsl:variable name="total">
>                    <xsl:call-template name="get-defs">
>                        <xsl:with-param name="list" select="$rest"/>
>                    </xsl:call-template>
>                </xsl:variable>
>               <xsl:variable name="def">
>                    <xsl:call-template name="get-def">
>                        <xsl:with-param name="term" select="$first"/>
>                    </xsl:call-template>
>                </xsl:variable>
>                <xsl:message><xsl:value-of select="$def"/></xsl:message>
>                <xsl:choose>
>                    <xsl:when test="$total=''">
>                        <xsl:value-of select="$def"/>
>                    </xsl:when>
>                    <xsl:otherwise>
>                        <xsl:value-of select="concat($def,', ',$total)"/>
>                    </xsl:otherwise>
>                </xsl:choose>
>            </xsl:when>
>        </xsl:choose>
>    </xsl:template>
>
>    <xsl:template name="get-def">
>        <xsl:param name="term"/>
>        <xsl:value-of select="//def[preceding-sibling::term=$term]"/>
>    </xsl:template>
></xsl:stylesheet>
>
>This works, but I have a couple of curiosities that I am trying to work
>through. If I change one of the terms so that the "get-def" template doesn't
>match (for example, "B" to "BB"), I get this:
>
><?xml version="1.0" encoding="UTF-8"?>
><root>
>   <defs>Def for A, , Def for C, Def for D, Def for E</defs>
></root>
>
>I thought I could use an <xsl:if test="$def!=''"> right before the last
><xsl:choose> statement, but when I do, I only get this:
>
><?xml version="1.0" encoding="UTF-8"?>
><root>
>   <defs>Def for A</defs>
></root>
>
>Once there is no match on "B" it short-circuits the rest of the list. Any
>help or guidance would be appreciated. Thanks.
>
>Rick Quatro
>
>Rick Quatro
>Carmen Publishing Inc.
>585-366-4017
>rick@xxxxxxxxxxxxxxx
>
>
>
>
>
>
>

XSL-List info and archive
EasyUnsubscribe (by email)


Current Thread
Keywords