xml - Output unique XSLT attribute values when using a key nested within another key -


i have been working on web page constructed through combination of xslt 1.0 , xml , come across problem have been struggling export unique attribute values while using xsl key (<xsl:keyname="name" match="pattern" use="expression"/>) nested within xsl key.

due sensitivity of xml data, have reconstructed problem using 'classic w3c schools example' of bookstore.

the code:

xsl:

<xsl:stylesheet   version="1.0"   xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:output indent="yes"/>  <xsl:key name="prom-by-id" match="bookstore/promotion" use="promotionid"/> <xsl:key name="location-by-id" match="bookstore/locations" use="locationid"/>  <xsl:template match="bookstore">   <xsl:apply-templates select="book"/> </xsl:template>  <xsl:template match="book">   <xsl:copy>      <p><strong><xsl:value-of select="title"/></strong></p>           <xsl:for-each select="key('prom-by-id', occurrences/promotions/promotionnum)">             <xsl:for-each select="key('location-by-id', promotionlocation)">               <p><xsl:value-of select="location"/></p>             </xsl:for-each>           </xsl:for-each>      <br/><br/>    </xsl:copy> </xsl:template> 

xml:

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="books.xsl" ?> <bookstore>   <book>     <title>harry potter , philosopher's stone</title>     <author>j k. rowling</author>     <year>1997</year>     <price>3.99</price>     <publisher>bloomsbury (uk)</publisher>     <synopsis>          harry potter , philosopher's stone first novel in harry potter series , j. k. rowling's debut novel.           plot follows harry potter, young wizard discovers magical heritage makes close friends , few enemies in first year @ hogwarts school of witchcraft , wizardry.           of friends, harry faces attempted comeback dark wizard lord voldemort, killed harry's parents, failed kill harry when year old.    </synopsis>    <occurrences>     <promotions>       <promotioncode>1</promotioncode>       <promotionnum>1</promotionnum>       <promotionnum>2</promotionnum>       <promotionnum>3</promotionnum>       <promotionnum>4</promotionnum>    </promotions>    </occurrences>  </book>  <book>     <title>the girl dragon tattoo</title>     <author>stieg larsson</author>     <year>2005</year>     <price>5.99</price>     <publisher>norstedts förlag (swe)</publisher>     <synopsis>          in stockholm, sweden, journalist mikael blomkvist, co-owner of millennium magazine, has lost libel case brought against him businessman hans-erik wennerström. lisbeth salander, brilliant troubled investigator , hacker, compiles extensive background check on blomkvist business magnate henrik vanger, has special task him.            in exchange promise of damning information wennerström, blomkvist agrees investigate disappearance , assumed murder of henrik's grandniece, harriet, 40 years ago.            after moving vanger family's compound, blomkvist uncovers notebook containing list of names , numbers no 1 has been able decipher.     </synopsis>     <occurrences>       <promotions>          <promotioncode>3</promotioncode>         <promotionnum>5</promotionnum>         <promotionnum>6</promotionnum>         <promotionnum>3</promotionnum>         <promotionnum>2</promotionnum>       </promotions>     </occurrences>   </book>    <book>     <title>grandpa's great escape</title>     <author>david walliams</author>     <year>2015</year>     <price>5.00</price>     <publisher>harper collins children's books</publisher>     <synopsis>          exquisite portrait of bond between small boy , beloved grandpa – book takes readers on incredible journey spitfires on london , great escapes through city in high octane adventure full of comedy , heart. illustrated award-winning tony ross.     </synopsis>     <occurrences>     <promotions>       <promotioncode>4</promotioncode>       <promotionnum>1</promotionnum>       <promotionnum>2</promotionnum>       <promotionnum>4</promotionnum>    </promotions>  </occurrences>   </book> <book>     <title>a brief history of 7 killings</title>     <author>marlon james</author>     <year>2015</year>     <price>6.29</price>     <publisher>oneworld publications</publisher>     <synopsis>          acclaimed author of book of night women comes dazzling display of masterful storytelling exploring near-mythic event. spanning 3 decades , crossing continents, brief history of 7 killings chronicles lives of host of unforgettable characters – slum kids, one-night stands, drug lords, girlfriends, gunmen, journalists, , cia.            gripping , inventive, ambitious , mesmerising, brief history of 7 killings 1 of remarkable , extraordinary novels of twenty-first century.     </synopsis>     <occurrences>       <promotions>          <promotioncode>5</promotioncode>         <promotionnum>1</promotionnum>         <promotionnum>5</promotionnum>         <promotionnum>6</promotionnum>       </promotions>     </occurrences>         <occurrences>       <promotions>          <promotioncode>5</promotioncode>         <promotionnum>1</promotionnum>         <promotionnum>5</promotionnum>         <promotionnum>6</promotionnum>       </promotions>     </occurrences>   </book>   <book>     <title>leading</title>     <author>sir alex ferguson</author>     <year>2015</year>     <price>7.99</price>     <publisher>hodder , stoughton</publisher>     <synopsis>          in revelatory new book, sir alex ferguson dissects 38 record-breaking years of management - first in scotland , then, of course, @ manchester united - reveal key tools used deliver sustained success on , off field. tactics teamwork, hiring firing, dealing boardroom responding failure, leading investigates pivotal leadership decisions of astonishing career.     </synopsis>     <occurrences>       <promotions>          <promotioncode>6</promotioncode>         <promotionnum>2</promotionnum>         <promotionnum>3</promotionnum>         <promotionnum>6</promotionnum>       </promotions>     </occurrences>   </book>   <book>     <title>the girl on train</title>     <author>paula hawkins</author>     <year>2015</year>     <price>6.99</price>     <publisher>doubleday</publisher>     <synopsis>          rachel catches same commuter train every morning. knows wait @ same signal each time, overlooking row of gardens. she’s started feel knows people live in 1 of houses. ‘jess , jason’, calls them. life – sees – perfect. if rachel happy.          , sees shocking. it’s minute until train moves on, it’s enough.           everything’s changed. rachel has chance become part of lives she’s watched afar.          they’ll see; she’s more girl on train…     </synopsis>     <occurrences>       <promotions>          <promotioncode>7</promotioncode>         <promotionnum>1</promotionnum>         <promotionnum>2</promotionnum>         <promotionnum>3</promotionnum>         <promotionnum>4</promotionnum>       </promotions>     </occurrences>   </book>    <promotion>     <promotionid>1</promotionid>     <percentageoff>10</percentageoff>     <promotionalmerchandise>no</promotionalmerchandise>     <promotionstartdate>2015-10-14t00:00:00</promotionstartdate>     <promotionenddate>2015-10-19t00:00:00</promotionenddate>     <promotionlocation>1</promotionlocation>   </promotion>   <promotion>     <promotionid>2</promotionid>     <percentageoff>15</percentageoff>     <promotionalmerchandise>no</promotionalmerchandise>     <promotionstartdate>2015-10-11t00:00:00</promotionstartdate>     <promotionenddate>2015-10-16t00:00:00</promotionenddate>     <promotionlocation>2</promotionlocation>   </promotion>   <promotion>     <promotionid>3</promotionid>     <percentageoff>30</percentageoff>     <promotionalmerchandise>yes</promotionalmerchandise>     <promotionstartdate>2015-09-02t00:00:00</promotionstartdate>     <promotionenddate>2015-09-07t00:00:00</promotionenddate>     <promotionlocation>2</promotionlocation>   </promotion>   <promotion>     <promotionid>4</promotionid>     <percentageoff>5</percentageoff>     <promotionalmerchandise>yes</promotionalmerchandise>     <promotionstartdate>2015-11-22t00:00:00</promotionstartdate>     <promotionenddate>2015-11-27t00:00:00</promotionenddate>     <promotionlocation>3</promotionlocation>   </promotion>   <promotion>     <promotionid>5</promotionid>     <percentageoff>50</percentageoff>     <promotionalmerchandise>no</promotionalmerchandise>     <promotionstartdate>2015-08-13t00:00:00</promotionstartdate>     <promotionenddate>2015-08-18t00:00:00</promotionenddate>     <promotionlocation>1</promotionlocation>   </promotion>   <promotion>     <promotionid>6</promotionid>     <percentageoff>80</percentageoff>     <promotionalmerchandise>no</promotionalmerchandise>     <promotionstartdate>2015-07-01t00:00:00</promotionstartdate>     <promotionenddate>2015-07-05t00:00:00</promotionenddate>     <promotionlocation>1</promotionlocation>   </promotion>    <locations>     <locationid>1</locationid>     <location>york</location>   </locations>     <locations>       <locationid>2</locationid>     <location>london</location>   </locations>     <locations>       <locationid>3</locationid>     <location>glasgow</location>   </locations> </bookstore> 

live example:

i have created live version of problem can viewed @ http://xsltransform.net/94rmq6j

the problem:

part one

for each <book/occurrences> attribute held in xml data, i'm using first <xsl:key> cross-reference every available <book/occurrences/promotionnum> value (number) against corresponding <bookstore/promotion/promotionid> value.

part two

if <bookstore/promotion/promotionid> match found, second <xsl:key> triggered targeting <bookstore/promotion/promotionlocation> search criteria , attempts match <bookstore/locations/locationid> value.

part three

if second , final match made value of <bookstore/locations/location> outputted providing name of city in uk.

unfortunately i'm struggling find way output unique <bookstore/locations/location> values, cities repeating 2 or 3 time per <book/occurrences>.

actual output:

<book>    <p>       <strong>harry potter , philosopher's stone</strong>    </p>    <p>york</p>    <p>london</p>    <p>london</p>    <p>glasgow</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>the girl dragon tattoo</strong>    </p>    <p>london</p>    <p>london</p>    <p>york</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>grandpa's great escape</strong>    </p>    <p>york</p>    <p>london</p>    <p>glasgow</p>    <br/>    <br/> </book> <book>    <p>       <strong>a brief history of 7 killings</strong>    </p>    <p>york</p>    <p>york</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>leading</strong>    </p>    <p>london</p>    <p>london</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>the girl on train</strong>    </p>    <p>york</p>    <p>london</p>    <p>london</p>    <p>glasgow</p>    <br/>    <br/> </book> 

desired output:

<book>    <p>       <strong>harry potter , philosopher's stone</strong>    </p>    <p>york</p>    <p>london</p>    <p>glasgow</p>    <br/>    <br/> </book> <book>    <p>       <strong>the girl dragon tattoo</strong>    </p>    <p>london</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>grandpa's great escape</strong>    </p>    <p>york</p>    <p>london</p>    <p>glasgow</p>    <br/>    <br/> </book> <book>    <p>       <strong>a brief history of 7 killings</strong>    </p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>leading</strong>    </p>    <p>london</p>    <p>york</p>    <br/>    <br/> </book> <book>    <p>       <strong>the girl on train</strong>    </p>    <p>york</p>    <p>london</p>    <p>glasgow</p>    <br/>    <br/> </book> 

so can fix problem? relate preceding-sibling or preceding in <xsl:key> or muenchian method approach work, using form of generate-id on <book/occurrences/promotionnum>? i'm sure 1 of 3 solutions i'm not sure which?

any appreciated in advance.

i think want

      <xsl:for-each select="key('location-by-id', key('prom-by-id', occurrences/promotions/promotionnum)/promotionlocation)">            <p><xsl:value-of select="location"/></p>          </xsl:for-each> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -