2

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

<c01>使用一些为许多s、 s 等创建表的 XSL,<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”)的 s。每个<container type>应该只出现一次<c01>,它应该从第一个中提取<c02>。有时<c02>s 只有一个s <container type>,有时<c01>s 有多个<c02>s 和<container type="box">& <container type="folder">

我已经尝试了许多变体position()=1和使用<xsl:choose>/ <xsl:when>,几乎所有我能想到的。它要么总是为&<th>的每个实例显示一个,要么每次有两个s 时显示s。<container type="box"><container type="folder"><th><container type>

有任何想法吗?

使用实际(不需要的)输出更新
我被要求提供一些所需/实际的 XML 输出。这是我传达实际 HTML 输出的最佳尝试(因为我无法复制/粘贴它),忽略所有内容(因为我已经弄清楚了那部分)<c01></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>
4

1 回答 1

1

好吧,我起初以为这是一个需要Muenchian 分组的分组问题。

但后来我仔细看了看,改变了主意。这是我的做法:

<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>
于 2012-09-21T02:08:19.603 回答