4

我对此束手无策,想知道这是否可以通过 XSL 实现。假设我有这个 XML 数据:

<?xml version="1.0" standalone="yes"?>
<Data>
  <Row>
    <F1>Created By</F1>    
    <F2>City</F2>
  </Row>
  <Row>
    <F1>John Doe</F1> 
    <F2>Los Angeles</F2>   
  </Row>
  <Row>
    <F1>Jane Doe</F1> 
    <F2>San Diego</F2>   
  </Row>
</Data>

我想在遍历其余数据时重复第一行元素。简而言之,我希望输出为:

[Created By] [City]
-----------  ------------
[John Doe]   [Los Angeles]

[Created By] [City]
----------   ------------
[Jane Doe]   [San Diego]

最好的方法是什么?我尝试将第一个元素“创建者”设置为变量,但是当我尝试使用它时它不会呈现。我是 XSL 的新手,任何帮助都将不胜感激。

谢谢

4

3 回答 3

4

这是一个解决方案,与当前接受的答案不同,它可以正确处理具有各种未知提前长度的数据——进一步参见处理无限数量列的更新

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vMax1">
  <xsl:call-template name="maxLength">
   <xsl:with-param name="pNodes" select="/*/*/*[1]"/>
  </xsl:call-template>
 </xsl:variable>
 <xsl:variable name="vMax2">
  <xsl:call-template name="maxLength">
   <xsl:with-param name="pNodes" select="/*/*/*[2]"/>
  </xsl:call-template>
 </xsl:variable>

 <xsl:variable name="vLongest1" select=
  "/*/*/*[1][string-length() = $vMax1][1]"/>
 <xsl:variable name="vLongest2" select=
  "/*/*/*[2][string-length() = $vMax2][1]"/>

 <xsl:variable name="vUnderscores1" select=
  "concat('__',
          translate($vLongest1,translate($vLongest1, '_', ''),
                    '_________________________________________________________')
          )"/>
 <xsl:variable name="vUnderscores2" select=
  "concat('__',
          translate($vLongest2,translate($vLongest2, '_', ''),
                    '_________________________________________________________')
          )"/>
 <xsl:variable name="vBlanks1"
   select="translate($vUnderscores1,'_', ' ')"/>
 <xsl:variable name="vBlanks2"
   select="translate($vUnderscores2,'_', ' ')"/>

 <xsl:variable name="vTitle1" select=
 "concat('[',/*/*/*[1],']',
        substring($vBlanks1,1,
                  string-length($vBlanks1)-string-length(/*/*/*[1]))
        )"/>
 <xsl:variable name="vTitle2" select=
 "concat('[',/*/*/*[2],']',
        substring($vBlanks2,1,
                  string-length($vBlanks2)-string-length(/*/*/*[2]))
        )"/>

 <xsl:template match="Row">
  <xsl:value-of select=
   "concat('&#xA;', $vTitle1, $vTitle2)"/>
  <xsl:value-of select=
   "concat('&#xA;',$vUnderscores1, '  ', $vUnderscores2, '&#xA;')"/>
  <xsl:value-of select=
   "concat(F1,
           substring($vBlanks1,1,
                     string-length($vBlanks1)-string-length(F1)),
           '  ',
           F2,
           substring($vBlanks1,1,
                     string-length($vBlanks1)-string-length(F2)),
           '&#xA;'
          )"/>
 </xsl:template>

 <xsl:template name="maxLength">
  <xsl:param name="pNodes" select="/.."/>

  <xsl:for-each select="$pNodes">
   <xsl:sort select="string-length()"
        data-type="number" order="descending"/>
   <xsl:if test="position() = 1">
    <xsl:value-of select="string-length()"/>
   </xsl:if>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="Row[1]|text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Data>
  <Row>
    <F1>Created By</F1>
    <F2>City</F2>
  </Row>
  <Row>
    <F1>John Doe</F1>
    <F2>Los Angeles</F2>
  </Row>
  <Row>
    <F1>Jane Doe</F1>
    <F2>San Diego</F2>
  </Row>
</Data>

产生了想要的正确结果:

[Created By]  [City]         
____________  _____________
John Doe      Los Angeles 

[Created By]  [City]         
____________  _____________
Jane Doe      San Diego   

更有趣的是,当应用于这个 XML 文档时:

<Data>
    <Row>
        <F1>Created By</F1>
        <F2>City</F2>
    </Row>
    <Row>
        <F1>John Doe</F1>
        <F2>La Villa Real de la Santa Fe de San Francisco de Asis</F2>
    </Row>
    <Row>
        <F1>Josiah Willard Gibbs</F1>
        <F2>San Diego</F2>
    </Row>
</Data>

再次产生正确的结果

[Created By]            [City]                                                   
______________________  _______________________________________________________
John Doe                La Villa Real de la Santa Fe de San Francisco de Asis

[Created By]            [City]                                                   
______________________  _______________________________________________________
Josiah Willard Gibbs    San Diego             

将此正确结果与当前接受的答案产生的结果进行比较:

Created By  City        
----------- ----------- 
John Doe    La Villa Real de la Santa Fe de San Francisco de Asis

Created By  City        
----------- ----------- 
Josiah Willard GibbsSan Diego   

II 相同解决方案的 XSLT 2.0 变体:更短且更易于编写

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:variable name="vMax" as="xs:integer*" select=
  "for $i in 1 to 2
    return
      max(/*/*/*[$i]/string-length(.))"/>

 <xsl:variable name="vUnderscores" select=
  "for $i in 1 to 2
    return
           concat('__',
                  string-join((for $len in 1 to $vMax[$i]
                                            return '_'), '')
                  )"/>

 <xsl:variable name="vBlanks" select=
  "for $i in 1 to 2
     return
       translate($vUnderscores[$i],'_', ' ')

  "/>

 <xsl:variable name="vTitle" select=
  "for $i in 1 to 2
    return
       concat('[',(*/*/*)[$i],']',
        substring($vBlanks[$i],1,
                  $vMax[$i]+2 -string-length((/*/*/*)[$i]))
        )

  "/>
 <xsl:template match="Row">
  <xsl:value-of select=
   "concat('&#xA;', $vTitle[1], $vTitle[2])"/>
  <xsl:value-of select=
   "concat('&#xA;',$vUnderscores[1], '  ', $vUnderscores[2], '&#xA;')"/>
  <xsl:value-of select=
   "concat(F1,
           substring($vBlanks[1],1,
                     $vMax[1]+2 -string-length(F1)),
           '  ',
           F2,
           substring($vBlanks[2],1,
                     $vMax[1]+2 -string-length(F2)),
           '&#xA;'
          )"/>
 </xsl:template>
 <xsl:template match="Row[1]|text()"/>
</xsl:stylesheet>

三、处理不确定的列数

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:variable name="vNumCols" select="max(/*/*/count(*))"/>

 <xsl:variable name="vMax" as="xs:integer*" select=
  "for $i in 1 to $vNumCols
    return
      max(/*/*/*[$i]/string-length(.))"/>

 <xsl:variable name="vUnderscores" select=
  "for $i in 1 to $vNumCols
    return
       concat('__',
              string-join((for $len in 1 to $vMax[$i]
                            return '_'), '')
             )"/>

 <xsl:variable name="vBlanks" select=
  "for $i in 1 to $vNumCols
     return
       translate($vUnderscores[$i],'_', ' ')

  "/>

 <xsl:variable name="vTitle" select=
  "for $i in 1 to $vNumCols
    return
       concat('[',(*/*/*)[$i],']',
        substring($vBlanks[$i],1,
                  $vMax[$i]+2 -string-length((/*/*/*)[$i]))
        )

  "/>
 <xsl:template match="Row">
  <xsl:value-of separator="" select=
   "'&#xA;', string-join($vTitle, '')"/>
  <xsl:value-of separator="" select=
   "'&#xA;', string-join($vUnderscores, '  '), '&#xA;'"/>

  <xsl:value-of select=
   "string-join((for $i in 1 to $vNumCols,
               $vChild in *[$i]
            return
              ($vChild,
               substring($vBlanks[$i],1,
                         $vMax[$i]+2 -string-length($vChild)
                        ),
              '  '
                     ),
                 '&#xA;'
                 ),
                 ''
               )"/>
 </xsl:template>
 <xsl:template match="Row[1]|text()"/>
</xsl:stylesheet>

应用于此 XML 文档(3 列)时

<Data>
  <Row>
    <F1>Created By</F1>
    <F2>City</F2>
    <F3>Region</F3>
  </Row>
  <Row>
    <F1>Pablo Diego Ruiz y Picasso</F1>
    <F2>Los Angeles</F2>
    <F3>CA</F3>
  </Row>
  <Row>
    <F1>Jane Doe</F1>
    <F2>La Villa Real de la Santa Fe de San Francisco de Asis</F2>
    <F3>NM</F3>
  </Row>
</Data>

产生了想要的正确结果:

[Created By]                  [City]                                                   [Region]  
____________________________  _______________________________________________________  ________
Pablo Diego Ruiz y Picasso    Los Angeles                                              CA        

[Created By]                  [City]                                                   [Region]  
____________________________  _______________________________________________________  ________
Jane Doe                      La Villa Real de la Santa Fe de San Francisco de Asis    NM     

四。第 III 部分(上)的 XSLT 1.0 的翻译:

请注意

JLRishe 提供的解决方案作为对他的初始答案的更正,如果有 100 行,每行有 4 列,则将执行 400 次排序。

下面的代码中没有这样的低效率

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vMaxCols">
  <xsl:call-template name="maxChildren">
   <xsl:with-param name="pNodes" select="/*/*"/>
  </xsl:call-template>
 </xsl:variable>

 <xsl:variable name="vrtfMax">
  <xsl:for-each select="(/*/*/*)[not(position() > $vMaxCols)]">
   <xsl:variable name="vPos" select="position()"/>
   <length>
     <xsl:call-template name="maxLength">
       <xsl:with-param name="pNodes" select="/*/*/*[position()=$vPos]"/>
     </xsl:call-template>
   </length>
  </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vMax" select="ext:node-set($vrtfMax)/length"/>

 <xsl:variable name="vrtfUnderscores">
  <xsl:for-each select="(/*/*/*)[not(position() > $vMaxCols)]">
    <xsl:variable name="vPos" select="position()"/>
    <xsl:variable name="vLongestDataNode" select=
     "/*/*/*[position()=$vPos
           and string-length() = $vMax[position() = $vPos]][1]"/>
    <t>
       <xsl:value-of select=
       "concat('__',
                   translate($vLongestDataNode,translate($vLongestDataNode, '_', ''),
                             '_________________________________________________________')
               )"/>
    </t>
  </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vUnderscores" select="ext:node-set($vrtfUnderscores)/t"/>

 <xsl:variable name="vrtfBlanks">
  <xsl:for-each select="$vUnderscores">
   <xsl:variable name="vPos" select="position()"/>
   <t><xsl:value-of select=
           "translate($vUnderscores[position()=$vPos],'_', ' ')"/>
   </t>
  </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vBlanks" select="ext:node-set($vrtfBlanks)/t"/>

 <xsl:variable name="vrtfTitle">
  <xsl:for-each select="/*/*[1]/*">
   <xsl:variable name="vPos" select="position()"/>
   <t>
     <xsl:value-of select=
     "concat('[',.,']',
        substring($vBlanks[position()=$vPos],1,
                  2+$vMax[position()=$vPos]-string-length())
        )
     "/>
   </t>
  </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vTitle" select="ext:node-set($vrtfTitle)/t"/>

 <xsl:template match="Row">
  <xsl:text>&#xA;</xsl:text>
  <xsl:for-each select="$vTitle">
      <xsl:value-of select="."/>
  </xsl:for-each>

  <xsl:text>&#xA;</xsl:text>
  <xsl:for-each select="$vUnderscores">
      <xsl:value-of select="."/>
      <xsl:text>  </xsl:text>
  </xsl:for-each>
  <xsl:text>&#xA;</xsl:text>

  <xsl:for-each select="*[not(position() > $vMaxCols)]">
    <xsl:variable name="vPos" select="position()"/>

    <xsl:value-of select=
     "concat(.,
           substring($vBlanks[position()=$vPos],
                     1,
                     string-length($vBlanks[position()=$vPos])
                     -string-length()),
           '  '
           )"/>
  </xsl:for-each>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>

 <xsl:template name="maxChildren">
  <xsl:param name="pNodes" select="/.."/>

  <xsl:for-each select="$pNodes">
   <xsl:sort select="count(*)"
        data-type="number" order="descending"/>
   <xsl:if test="position() = 1">
    <xsl:value-of select="count(*)"/>
   </xsl:if>
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="maxLength">
  <xsl:param name="pNodes" select="/.."/>

  <xsl:for-each select="$pNodes">
   <xsl:sort select="string-length()"
        data-type="number" order="descending"/>
   <xsl:if test="position() = 1">
    <xsl:value-of select="string-length()"/>
   </xsl:if>
  </xsl:for-each>
 </xsl:template>
 <xsl:template match="Row[1]|text()"/>
</xsl:stylesheet>
于 2013-03-16T18:41:47.633 回答
2

这是否足够:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"/>
  <xsl:key name="kColumn" match="Row/*" use="count(preceding-sibling::*) + 1" />

  <xsl:param name="columnWidth" select="13" />

  <xsl:variable name="firstRow" select="/Data/Row[1]" />
  <xsl:variable name="spaces" 
                select="'                                                                           '" />
  <xsl:variable name="separatorChars"
                select="'---------------------------------------------------------------------------'" />

  <xsl:template match="/*">
    <xsl:apply-templates select="Row[position() > 1]" />
  </xsl:template>

  <xsl:template match="Row">
    <xsl:apply-templates select="$firstRow/*" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates select="$firstRow/*" mode="separator" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates select="*"/>
    <xsl:text>&#xA;&#xA;</xsl:text>
  </xsl:template>

  <xsl:template match="Row/*" name="Cell">
    <xsl:param name="value" select="concat('[', ., ']')" />
    <xsl:param name="width">
      <xsl:call-template name="FindWidth" />
    </xsl:param>

    <xsl:variable name="numSpaces"
                  select="$width + 1 - string-length($value)" />
    <xsl:variable name="trailingSpace" select="substring($spaces, 1, $numSpaces)" />
    <xsl:value-of select="concat($value, $trailingSpace)"/>
  </xsl:template>

  <xsl:template match="Row/*" mode="separator">
    <xsl:variable name="width">
      <xsl:call-template name="FindWidth" />
    </xsl:variable>
    <xsl:call-template name="Cell">
      <xsl:with-param name="value" select="substring($separatorChars, 1, $width)" />
      <xsl:with-param name="width" select="$width" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="FindWidth">
    <xsl:apply-templates select="key('kColumn', position())" mode="findLength">
      <xsl:sort select="string-length()" data-type="number" order="descending" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Row/*" mode="findLength">
    <xsl:if test="position() = 1">
      <xsl:variable name="len" select="string-length() + 2" />
      <xsl:value-of select="$len * ($len > $columnWidth) + $columnWidth * ($columnWidth > $len)"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

在此输入上运行时:

<Data>
  <Row>
    <F1>Created By</F1>
    <F2>City</F2>
    <F3>Region</F3>
  </Row>
  <Row>
    <F1>Pablo Diego Ruiz y Picasso</F1>
    <F2>Los Angeles</F2>
    <F3>CA</F3>
  </Row>
  <Row>
    <F1>Jane Doe</F1>
    <F2>La Villa Real de la Santa Fe de San Francisco de Asis</F2>
    <F3>NM</F3>
  </Row>
</Data>

它产生:

[Created By]                 [City]                                                  [Region]      
---------------------------- ------------------------------------------------------- ------------- 
[Pablo Diego Ruiz y Picasso] [Los Angeles]                                           [CA]          

[Created By]                 [City]                                                  [Region]      
---------------------------- ------------------------------------------------------- ------------- 
[Jane Doe]                   [La Villa Real de la Santa Fe de San Francisco de Asis] [NM]          
于 2013-03-16T17:14:12.097 回答
1

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Output HTML - as an example -->
  <xsl:output method="html" indent="yes"/>

  <!-- Template that skips the first row, so it does not get processed -->
  <xsl:template match="Row[position()=1]">
  </xsl:template>

  <!-- Template that process all rows except the first one -->
  <xsl:template match="Row[position()>1]">
    <!-- Copy the first row-->
    <tr>
      <xsl:for-each select="../Row[1]/*">
        <td>
          <xsl:value-of select="."/>
        </td>
      </xsl:for-each>
    </tr>
    <!-- Process this row -->
    <tr>
      <xsl:for-each select="*">
        <td>
          <xsl:value-of select="."/>
        </td>
      </xsl:for-each>
    </tr>
  </xsl:template>

  <!-- Root template: output the HTML scaffolding and then processes the individual rows -->
  <xsl:template match="/">
    <html>
      <head></head>
      <body>
        <table>
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

它输出一个 HTML 表格作为示例 - 它应该很容易适应其他输出。

于 2013-03-16T17:20:16.370 回答