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

Re: [xsl] constructing an element with an element's content based on the content of sibling element


Subject: Re: [xsl] constructing an element with an element's content based on the content of sibling element
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Sun, 10 Jun 2012 22:01:48 -0400

I'm not entirely sure I understand the desired logic, but I think
it's "when I process a <product>, identify it with a <controlfield>
that has (as content) the ISBN13 if there is one, the ISBN10 if not". 

It would be easier to tell if you'd provide a complete well-formed
test input file.

In any case, I think the attached does what you want in a more
XSLT-ish way.

But it's worth noting that:

>    <xsl:choose>
>       <xsl:when test="not( WHATEVER )">
>         STUFF
>       </xsl:when>
>       <xsl:otherwise></xsl:otherwise>
>    </xsl:choose>
> 
>    <xsl:choose>
>       <xsl:when test="WHATEVER">
>         OTHERSTUFF
>       </xsl:when>
>       <xsl:otherwise></xsl:otherwise>
>    </xsl:choose>

is the same as

  <xsl:choose>
    <xsl:when test="WHATEVER">
      OTHERSTUFF
    </xsl:when>
    <xsl:otherwise>
      STUFF
    </xsl:otherwise>
  </xsl:choose>

It would probably be a good idea to read the posting guidelines:
  http://www.mulberrytech.com/xsl/xsl-list/#posting

In any case, here's one way to do what you want. There are definitely
other ways, some probably better.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:template match="/wrapper">
    <wrapper>
      <xsl:apply-templates select="product"/>
    </wrapper>
  </xsl:template>

  <xsl:template match="product">
    <xsl:choose>
      <xsl:when test="productidentifier/b221 = '15'">
        <xsl:apply-templates select="productidentifier[b221='15']"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="productidentifier[b221='02']"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="productidentifier[b221 eq '02']">
    <controlfield tag="001">
      <xsl:text>ISBN10=</xsl:text>
      <xsl:value-of select="b244"/>
    </controlfield>
  </xsl:template>

  <xsl:template match="productidentifier[b221 eq '15']">
    <controlfield tag="001">
      <xsl:text>ISBN13=</xsl:text>
      <xsl:value-of select="b244"/>
    </controlfield>
  </xsl:template>

</xsl:stylesheet>

> Hoping someone can point out my misunderstanding of the solution in
> my transformation to the construction of an element,
> <controlfield>, with a preferred ISBN and the use of a second ISBN
> (13 characters vs. 10) if the preferred is unavailable; otherwise,
> nothing.
> 
> <productidentifier>
>  <b221>02</b221>
>  <b244>160542868X</b244>
> </productidentifier>
> <productidentifier>
>  <b221>03</b221>
>  <b244>9781605428680</b244>
> </productidentifier>
> <productidentifier>
>  <b221>15</b221>
>  <b244>9781605428680</b244>
> </productidentifier>
> 
> All of the product records have 3 productidentifiers with b221
> element values in the following order: 02, 03, 15.
> 
> To test the element construction with the second ISBN (b221=02) if
> the preferred ISBN (b221=15) is missing, I removed the
> productidentifer with element b221=15 from the second product
> record; I removed all productidentifiers from the third record.
> 
> <xsl:for-each select="productidentifier">
>   <xsl:variable name="isbn13">
>     <xsl:choose>
>        <xsl:when test="b221='15'">
>          <xsl:value-of select="b244"/>
>        </xsl:when>
>        <xsl:otherwise></xsl:otherwise>
>     </xsl:choose>
>   </xsl:variable>
>   <xsl:variable name="isbn10">
>     <xsl:choose>
>        <xsl:when test="b221='02'">
>          <xsl:value-of select="b244"/>
>        </xsl:when>
>        <xsl:otherwise></xsl:otherwise>
>     </xsl:choose>
>    </xsl:variable>
>                       
>    <xsl:choose>
>       <xsl:when test="not(empty($isbn13))">
>         <controlfield tag="001">
>         <xsl:text>xyz</xsl:text><xsl:value-of select="$isbn13"/>
>         </controlfield>
>       </xsl:when>
>       <xsl:otherwise></xsl:otherwise>
>    </xsl:choose>
> 
>    <xsl:choose>
>       <xsl:when test="empty($isbn13)">
>         <controlfield tag="001">
>         <xsl:text>xyz</xsl:text><xsl:value-of select="$isbn10"/>
>         </controlfield>
>       </xsl:when>
>       <xsl:otherwise></xsl:otherwise>
>    </xsl:choose>
> 
> </xsl:for-each>
> 
> The results of  the three records:
> 
> record 1
> 
>     <controlfield tag="001">xyz</controlfield>
>     <controlfield tag="001">xyz</controlfield>
>     <controlfield tag="001">xyz9781605428680</controlfield>
> 
> record 2
> 
>     <controlfield tag="001">xyz</controlfield>
>     <controlfield tag="001">xyz</controlfield>
> 
> record 3 has the desired result of no controlfield tag=001
> 
> I am apparently not accounting for productidentifier/b221=03 but
> have tried creating a similar variable which would result in an
> empty string...have tried using exists()...not using
> variables...but always get the same result..
> 
> hope the problem is clear...a little cross-eyed at this point..
> 
> any help would be greatly appreciated...


Current Thread