2016-02-05 46 views
2

我一直在努力通過XSLT 1.0XML及組合構建的網頁上的一個鍵時,對面,我一直在努力出口唯一一個問題來了屬性值,同時使用嵌套在另一個XSL鍵中的XSL Key<xsl:keyname="name" match="pattern" use="expression"/>)。輸出獨特的XSLT屬性值使用嵌套在另一個關鍵

由於我的XML數據的敏感性,我使用書店的'經典W3C學校示例'精確地重構了我的問題。

驗證碼:

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 and the Philosopher's Stone</title> 
    <author>J K. Rowling</author> 
    <year>1997</year> 
    <price>3.99</price> 
    <publisher>Bloomsbury (UK)</publisher> 
    <synopsis> 
     Harry Potter and the Philosopher's Stone is the first novel in the Harry Potter series and J. K. Rowling's debut novel. 

     The plot follows Harry Potter, a young wizard who discovers his magical heritage as he makes close friends and a few enemies in his first year at the Hogwarts School of Witchcraft and Wizardry. 

     With the help of his friends, Harry faces an attempted comeback by the dark wizard Lord Voldemort, who killed Harry's parents, but failed to kill Harry when he was just a 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 with the 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 a libel case brought against him by businessman Hans-Erik Wennerström. Lisbeth Salander, a brilliant but troubled investigator and hacker, compiles an extensive background check on Blomkvist for business magnate Henrik Vanger, who has a special task for him. 

     In exchange for the promise of damning information about Wennerström, Blomkvist agrees to investigate the disappearance and assumed murder of Henrik's grandniece, Harriet, 40 years ago. 

     After moving to the Vanger family's compound, Blomkvist uncovers a notebook containing a list of names and numbers that no one has been able to 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> 
     An exquisite portrait of the bond between a small boy and his beloved Grandpa – this book takes readers on an incredible journey with Spitfires over London and Great Escapes through the city in a high octane adventure full of comedy and heart. Illustrated by the 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 Seven Killings</title> 
    <author>Marlon James</author> 
    <year>2015</year> 
    <price>6.29</price> 
    <publisher>Oneworld Publications</publisher> 
    <synopsis> 
     From the acclaimed author of The Book of Night Women comes a dazzling display of masterful storytelling exploring this near-mythic event. Spanning three decades and crossing continents, A Brief History of Seven Killings chronicles the lives of a host of unforgettable characters – slum kids, one-night stands, drug lords, girlfriends, gunmen, journalists, and even the CIA. 

     Gripping and inventive, ambitious and mesmerising, A Brief History of Seven Killings is one of the most remarkable and extraordinary novels of the 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 and Stoughton</publisher> 
    <synopsis> 
     In this revelatory new book, Sir Alex Ferguson dissects his 38 record-breaking years of management - first in Scotland and then, of course, at Manchester United - to reveal the key tools he used to deliver sustained success on and off the field. From tactics to teamwork, from hiring to firing, from dealing with the boardroom to responding to failure, LEADING investigates the pivotal leadership decisions of an 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 the Train</title> 
    <author>Paula Hawkins</author> 
    <year>2015</year> 
    <price>6.99</price> 
    <publisher>Doubleday</publisher> 
    <synopsis> 
     Rachel catches the same commuter train every morning. She knows it will wait at the same signal each time, overlooking a row of back gardens. She’s even started to feel like she knows the people who live in one of the houses. ‘Jess and Jason’, she calls them. Their life – as she sees it – is perfect. If only Rachel could be that happy. 

     And then she sees something shocking. It’s only a minute until the train moves on, but it’s enough. 

     Now everything’s changed. Now Rachel has a chance to become a part of the lives she’s only watched from afar. 

     Now they’ll see; she’s much more than just the girl on the 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> 

活生生的例子:

我創造了這個問題,它可以被看作@http://xsltransform.net/94rmq6J

問題的一個活版本:

第一部分

對於保存的每個<book/occurrences>屬性在我的XML數據中,我使用我的第一個<xsl:key>來交叉引用每個可用的<book/occurrences/promotionNum>值(數字)值爲<bookstore/promotion/promotionID>

第二部分

如果<bookstore/promotion/promotionID>找到匹配,那麼第二<xsl:key>被觸發瞄準<bookstore/promotion/promotionLocation>作爲搜索條件,這將嘗試最後匹配任何<bookstore/locations/locationID>值。

第三部分

如果第二個和最後一場比賽,就這樣的<bookstore/locations/location>值輸出提供在英國一個城市的名稱。

不幸的是,我正在努力尋找一種方法來輸出唯一的<bookstore/locations/location>值,而城市往往每個<book/occurrences>重複兩到三次。

實際輸出:

<book> 
    <p> 
     <strong>Harry Potter and the 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 with the 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 Seven 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 the Train</strong> 
    </p> 
    <p>York</p> 
    <p>London</p> 
    <p>London</p> 
    <p>Glasgow</p> 
    <br/> 
    <br/> 
</book> 

所需的輸出:

<book> 
    <p> 
     <strong>Harry Potter and the Philosopher's Stone</strong> 
    </p> 
    <p>York</p> 
    <p>London</p> 
    <p>Glasgow</p> 
    <br/> 
    <br/> 
</book> 
<book> 
    <p> 
     <strong>The Girl with the 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 Seven 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 the Train</strong> 
    </p> 
    <p>York</p> 
    <p>London</p> 
    <p>Glasgow</p> 
    <br/> 
    <br/> 
</book> 

所以我能做些什麼來解決這個問題?它是否與preceding-siblingpreceding在我的<xsl:key>Muenchian Method方法工作,使用某種形式的generate-id<book/occurrences/promotionNum>?我確定這是三種解決方案之一,但我不確定哪一種?

任何幫助將不勝感激提前。

回答

2

我覺得你只是想

 <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>