XSL在循环中包含非循环元素

时间:2016-05-17 15:12:19

标签: xslt

我有一个看起来像这样的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="gallring.xsl"?>
<report>
<title>Bibliography</title>
 <dateCreated>2016-05-17</dateCreated>
<catalog>
 <catalogKey>142343</catalogKey>
 <yearOfPublication>1936</yearOfPublication>
 <marc>
  <marcEntry tag="100" label="Personal Author" ind="1 ">Landelius, Carl</marcEntry>
  <marcEntry tag="245" label="Title" ind="00">1840-1850-talets bildningscirklar och arbetareföreningar i Sverige. 1</marcEntry>
 </marc>
 <call>
  <callNumber>374</callNumber>
  <library>VALLA</library>
  <item>
   <dateLastUsed>2009-01-06</dateLastUsed>
  </item>
 </call>
</catalog>
<catalog>
 <catalogKey>661763</catalogKey>
 <yearOfPublication>1936</yearOfPublication>
 <marc>
  <marcEntry tag="100" label="Personal Author" ind="1 ">Landelius, Carl</marcEntry>
  <marcEntry tag="245" label="Title" ind="00">1840-1850-talets bildningscirklar och arbetareföreningar i Sverige / Carl Landelius</marcEntry>
 </marc>
 <call>
  <callNumber>374</callNumber>
  <library>VALLA</library>
  <item>
   <dateLastUsed>2014-06-18</dateLastUsed>
  </item>
 </call>
</catalog>
<catalog>
 <catalogKey>32018</catalogKey>
 <yearOfPublication>1982</yearOfPublication>
 <marc>
  <marcEntry tag="245" label="Title" ind="00">ABF-are berättar : minnen från ABF / red.: Allan Malmgren</marcEntry>
 </marc>
 <call>
  <callNumber>374</callNumber>
  <library>VALLA</library>
  <item>
   <dateLastUsed>2008-06-17</dateLastUsed>
  </item>
 </call>
</catalog>

要打开它我使用这个样式表

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <catalog>
    <xsl:for-each select="report/catalog">
        <itemline>
            <callNumber><xsl:text>"</xsl:text><xsl:value-of select="string(call/callNumber)"/><xsl:text>"</xsl:text></callNumber>
            <yearOfPublication><xsl:value-of select="yearOfPublication"/></yearOfPublication>
            <Author><xsl:value-of select="marc/marcEntry[@tag='100']"/></Author>
            <Title><xsl:value-of select="substring(marc/marcEntry[@tag='245'],1,30)"/></Title>
            <dateLastUsed><xsl:value-of select="call/item/dateLastUsed"/></dateLastUsed>
        </itemline>
    </xsl:for-each>
    </catalog>
</xsl:template>
</xsl:stylesheet>

问题是:我希望在每次调用/ item / dateLastUsed-element之后包含元素title / dateCreated,这样我就可以计算(在Excel中)自创建报表以来dateLastUsed过去了多少天。

1 个答案:

答案 0 :(得分:0)

有多种方法可以包含dateCreated,例如你可以添加

<dateCreated><xsl:value-of select="../dateCreated"/></dateCreated>

当您处于每个for-each的{​​{1}}循环时,这比当前循环向上一步并从当前report/catalog获取信息。 如果“真实”XML在此示例中仅包含一个report,则还可以编写

report

直接从根目标定位<dateCreated><xsl:value-of select="//report/dateCreated"/></dateCreated>