';循环';通过多个XML元素(同名)使用XSL 1.0检索最大的集合节点值

';循环';通过多个XML元素(同名)使用XSL 1.0检索最大的集合节点值,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我正在使用一个书店示例生成一个简单的HTML网页,并通过XML数据和XSLT(1.0)的组合输出我的网页内容,但最近我遇到了一个问题,我希望从StackOverflow社区获得一些反馈和建议 背景: <bookstore> <book> <title>Harry Potter and the Philosopher's Stone</title> <author>J K. Rowling</author>

我正在使用一个书店示例生成一个简单的HTML网页,并通过XML数据和XSLT(1.0)的组合输出我的网页内容,但最近我遇到了一个问题,我希望从StackOverflow社区获得一些反馈和建议

背景:

<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>
   <promotions>
    <promotionNum>1</promotionNum>
    <promotionNum>2</promotionNum>
    <promotionNum>3</promotionNum>
    <promotionNum>4</promotionNum>
   </promotions>
 </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>
    <promotions>
      <promotionNum>5</promotionNum>
      <promotionNum>6</promotionNum>
      <promotionNum>3</promotionNum>
      <promotionNum>2</promotionNum>
   </promotions>
  </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>
  </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>
  </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>
  </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>
  </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>
  </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>
  </promotion>
</bookstore>
在我的书店示例中,我有一系列名为
的主要元素,其中包含有关出版物的重要细节,如

现在,在每个
元素中,我有一个更重要的元素,名为
,它包含单个ID号(
),作为主键链接到另一个元素中的相应节点,该元素更详细地处理促销

XML结构:

<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>
   <promotions>
    <promotionNum>1</promotionNum>
    <promotionNum>2</promotionNum>
    <promotionNum>3</promotionNum>
    <promotionNum>4</promotionNum>
   </promotions>
 </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>
    <promotions>
      <promotionNum>5</promotionNum>
      <promotionNum>6</promotionNum>
      <promotionNum>3</promotionNum>
      <promotionNum>2</promotionNum>
   </promotions>
  </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>
  </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>
  </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>
  </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>
  </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>
  </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>
  </promotion>
</bookstore>

哈利·波特与魔法石
J K.罗琳
1997
3.99
布卢姆斯伯里(英国)
《哈利波特与魔法石》是哈利波特系列的第一部小说,也是J.K.罗琳的处女作。
故事发生在哈利·波特之后,这位年轻的巫师在霍格沃茨魔法学校的第一年就发现了自己的魔法遗产,结交了密友和几个敌人。
在朋友们的帮助下,哈利面临着黑巫师伏地魔的卷土重来,伏地魔杀死了哈利的父母,但在哈利一岁的时候却没能杀死他。
1.
2.
3.
4.
有龙纹身的女孩
斯蒂格·拉森
2005
5.99
Norstedts Förlag(瑞典)
在瑞典斯德哥尔摩,记者Mikael Blomkvist,Millennium杂志的共同拥有者,在商人Hans-Erik Wennerström提起的诽谤案中败诉。Lisbeth Salander是一位才华横溢但麻烦重重的调查员和黑客,她为商业巨头Henrik Vanger编写了一份关于Blomkvist的广泛背景调查报告,Henrik Vanger有一项特殊任务要做。
作为交换,布洛姆克维斯特承诺提供有关温内尔斯特伦的该死的信息,他同意调查亨里克的侄女哈丽特40年前失踪并被假定谋杀的事件。
在搬进Vanger家族的大院后,Blomkvist发现了一个笔记本,里面有一个没有人能破译的名字和数字列表。
5.
6.
3.
2.
1.
10
不
2015-10-14T00:00:00
2015-10-19T00:00:00
2.
15
不
2015-10-11T00:00:00
2015-10-16T00:00:00
3.
30
对
2015-09-02T00:00:00
2015-09-07T00:00:00
4.
5.
对
2015-11-22T00:00:00
2015-11-27T00:00:00
5.
50
不
2015-08-13T00:00:00
2015-08-18T00:00:00
6.
80
不
2015-07-01T00:00:00
2015-07-05T00:00:00
我想要实现什么?

对于每个
元素,我想使用
语句或
捕获变量中的
数字ID,例如
$IDNum
,循环相关的

然后使用我的ID变量作为验证,我想对照六个
元素交叉引用这个数字。如果我们成功地进行了匹配,例如
,那么我想在另一个变量中捕获
节点的值

但重要的是,我想找到最新日期(或最大值)。因此,对于《哈利·波特与魔法石》(1-4),我希望从《哈利·波特与魔法石》中检索四个相关的促销元素(1-4)

我知道XSLT 1.0与XSLT 2.0不同,XSLT 1.0和日期不兼容,因此在创建变量以存储
时,需要在“t”之后进行子字符串处理,然后进行翻译处理以丢失连字符,从而为您提供一个可使用的有效数字,例如
2015-11-27T00:00:00
变为
20151127
(即数字)

为什么?我想知道在任何选定的出版物上进行促销的最后日期

使用XSLT 1.0是否可以实现这一点?对于每个
节点,我是否可以运行substring/translate处理,然后使用一个模板在变量中捕获此日期数字,然后将其与通过模板传递的下一个可用日期值进行比较。如果此新数字比以前的
gt;
,则成为新的最高数字


如往常一样,任何建议都会受到热烈欢迎!!

使用交叉引用键,然后在该日期对它们进行排序,以找到最新的(最高编号):



您说过希望将HTML作为输出,但由于您没有显示为
书籍
元素创建HTML的任何XSLT代码,我选择只输出
书籍
本身及其交叉引用的
促销
,当然您可以将模板更改为输出HTML,找到促销的方法是:也一样。

这个解决方案正是我想要的。我没有想过使用键作为交叉引用的方法。对于任何感兴趣的人来说,为了在YEARMONTHDAY获得输出,例如20151127(没有时间戳的尾随零),我将Martin的代码行
交换为