我是XSL的新手并且在我学习的同时学习。我目前正在编辑本地创建的XML的第三方样式表,其中一部分如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl">
<c01>
<did>
<unittitle>Annual Reports</unittitle>
<physdesc>19 folders</physdesc>
</did>
<scopecontent>
<p>Annual reports...</p>
</scopecontent>
<c02>
<did>
<container type="box">1</container>
<container type="folder">1</container>
<unittitle>1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)</unittitle>
</did>
</c02>
<c02>
<did>
<container type="folder">2</container>
<unittitle>1869, 1872 (BPA); 1873 (IAS)</unittitle>
</did>
</c02>
<c01>
<did>
<unittitle>Bulletins</unittitle>
<physdesc>2 folders</physdesc>
</did>
<scopecontent>
<p>Bulletins...</p>
</scopecontent>
<c02>
<did>
<container type="box">1</container>
<container type="folder">21</container>
<unittitle>Bulletins 1945-46</unittitle>
</did>
</c02>
<c02>
<did>
<container type="box">2</container>
<container type="folder">1</container>
<unittitle>Bulletins 1946-51</unittitle>
</did>
</c02>
</c01>
使用一些XSL为许多<c01>
,<c02>
等创建一个表,看起来像这样:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
<xsl:template match="c02/did">
<tr>
<xsl:choose>
<xsl:when test="ancestor::c01/descendant::c02/did/container">
<xsl:if test="position()=1">
<th><xsl:value-of select="container[1]/@type"/></th>
<th><xsl:value-of select="container[2]/@type"/></th>
</xsl:if>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</tr>
</xsl:template>
在XSL中创建此模板之前的表创建了多个列,其中前两个是<th>
个拉<container type>
(通常是“Box”和“Folder”) )。每个<container type>
应该只显示一次<c01>
,它应该从第一个<c02>
开始。有时<c02>
只有一个<container type>
,有时<c01>
有<c02>
个<container type="box">
和<container type="folder">
。 position()=1
。
我尝试了<xsl:choose>
的许多变种并使用<xsl:when>
/ <th>
,几乎是我能想到的一切。它始终为<container type="box">
&amp;的每个实例显示<container type="folder">
。每次有<th>
次时<container type>
或显示<c01>
。
有什么想法吗?
使用实际(不需要的)输出进行更新
我被要求提供一些所需/实际的XML输出。这是我传达实际HTML输出的最佳尝试(因为我无法复制/粘贴它),忽略了从</scopecontent>
到<tr>
<th>Box</th>
<th>Folder</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)</td>
</tr>
<tr>
<th>Folder</th>
</tr>
<tr>
<td></td>
<td>2</td>
<td>1869, 1872 (BPA); 1873 (IAS)</td>
</tr>
...
<tr>
<th>Box</th>
<th>Folder</th>
</tr>
<tr>
<td>1</td>
<td>21</td>
<td>Bulletins 1945-46</td>
</tr>
<tr>
<th>Box</th>
<th>Folder</th>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>Bulletins 1946-51</td>
</tr>
的所有内容(因为我已经找到了这一部分):
<tr>
<th>Box</th>
<th>Folder</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)</td>
</tr>
<tr>
<td></td>
<td>2</td>
<td>1869, 1872 (BPA); 1873 (IAS)</td>
</tr>
...
<tr>
<th>Box</th>
<th>Folder</th>
</tr>
<tr>
<td>1</td>
<td>21</td>
<td>Bulletins 1945-46</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>Bulletins 1946-51</td>
</tr>
所需的输出
{{1}}
答案 0 :(得分:1)
嗯,我一开始认为这是一个需要Muenchian grouping的分组问题。
然后我仔细观察并改变主意。我就是这样做的:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="ISO-8859-1"
doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" />
<xsl:template match="c01">
<xsl:variable name="c01ID" select="generate-id()" />
<tr>
<xsl:for-each select="c02[1]//container">
<th>
<xsl:value-of select="@type" />
</th>
</xsl:for-each>
</tr>
<xsl:apply-templates select="c02" />
</xsl:template>
<xsl:template match="c02/did">
<tr>
<xsl:variable name="contextDid" select="." />
<xsl:for-each select="ancestor::c01/c02[1]//container/@type">
<xsl:variable name="currentType" select="." />
<td>
<xsl:value-of
select="$contextDid/container[@type = $currentType]/text()" />
</td>
</xsl:for-each>
<xsl:for-each select="unittitle">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
针对以下示例输入运行时:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?>
<root>
<c01>
<did>
<unittitle>Annual Reports</unittitle>
<physdesc>19 folders</physdesc>
</did>
<scopecontent>
<p>Annual reports...</p>
</scopecontent>
<c02>
<did>
<container type="box">1</container>
<container type="folder">1</container>
<unittitle>1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)</unittitle>
</did>
</c02>
<c02>
<did>
<container type="folder">2</container>
<unittitle>1869, 1872 (BPA); 1873 (IAS)</unittitle>
</did>
</c02>
</c01>
<c01>
<did>
<unittitle>Bulletins</unittitle>
<physdesc>2 folders</physdesc>
</did>
<scopecontent>
<p>Bulletins...</p>
</scopecontent>
<c02>
<did>
<container type="box">1</container>
<container type="folder">21</container>
<unittitle>Bulletins 1945-46</unittitle>
</did>
</c02>
<c02>
<did>
<container type="box">2</container>
<container type="folder">1</container>
<unittitle>Bulletins 1946-51</unittitle>
</did>
</c02>
</c01>
</root>
它产生所需的输出:
<tr>
<th>box</th>
<th>folder</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)</td>
</tr>
<tr>
<td></td>
<td>2</td>
<td>1869, 1872 (BPA); 1873 (IAS)</td>
</tr>
<tr>
<th>box</th>
<th>folder</th>
</tr>
<tr>
<td>1</td>
<td>21</td>
<td>Bulletins 1945-46</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>Bulletins 1946-51</td>
</tr>