XSLT:需要使用Excel单元格宽度XSLT

问题描述:

我需要使用XSLT 我输入XML到XML转换成XML来计算左,右是:XSLT:需要使用Excel单元格宽度XSLT

<sheets> 
    <Cells> 
     <rows rowNumber="0"> 
     <columns No="0"> 
      <cell id="A1" cellWidth="152" cellHeight="20" >test</cell> 
     </columns> 
     <columns No="1"> 
      <cell id="A2" cellWidth="52" cellHeight="20" >test1</cell> 
     </columns> 
     <columns No="2"> 
      <cell id="A3" cellWidth="50" cellHeight="40" >test1</cell> 
     </columns> 
     </rows> 
     <rows rowNumber="1"> 
     <columns No="0"> 
      <cell id="B1" cellWidth="50" cellHeight="20" >test</cell> 
     </columns> 
     <columns No="1"> 
      <cell id="B2" cellWidth="50" cellHeight="40" >test1</cell> 
     </columns> 
     <columns No="2"> 
      <cell id="B3" cellWidth="51" cellHeight="20" >test2</cell> 
     </columns> 
     </rows> 
     </Cells> 
     </Sheets> 

我的预期成果是:

<page> 
<line l="0" r="152" t="0" b="20">test</line> 
<line l="153" r="204" t="0" b="20">test1</line> 
<line l="205" r="254" t="0" b="40">test1</line> 
<line l="0" r="50" t="20" b="40">test</line> 
<line l="51" r="100" t="20" b="60">test1</line> 
<line l="101" r="151" t="40" b="60">test1</line> 
</page> 
现在

,问题是:
对于使用宽度计算左边和右边的值,我需要保持两件事情:
1至电池总行宽
2.我从单元格属性中获取的单元格宽度cellWidth
使用上面的两个参数,我可以计算左右两个参数
在用于单元格A3
总计行宽度以上实施例:254
细胞宽度:50
所以左= 254-50
和右= 254
类似地,使用细胞身高
计算顶部和底部值但在这种情况下,我需要计算逐列数据,我不能明白,我怎么能做到这一点使用

我曾尝试:
XSLT sum preceding siblings on attributes
How to sum the values of several nodes in XSLT
XSLT: Sum of all previous attributes

我的XSLT代码:

     <xsl:for-each select="columns"> 
          <xsl:variable name="totalWidth" select="sum(preceding-sibling::cell/@cellWidth)"/> 
          <xsl:variable name="totalHeight" select="sum(preceding-sibling::cell/@cellWidth)"/> 
          <line> 
              <xsl:attribute name="l"> 
            <xsl:value-of select="$totalWidth" /> 
           </xsl:attribute> 
           <xsl:attribute name="r"> 
            <xsl:value-of select="(round(cell/@cellWidth)+$totalWidth)" /> 
           </xsl:attribute> 

           <xsl:attribute name="t"> 
            <xsl:value-of select="$totalHeight" /> 
           </xsl:attribute> 

           <xsl:attribute name="b"> 
            <xsl:value-of select="round(cell/@cellHeight)+$totalHeight" /> 
           </xsl:attribute> 
              <xsl:value-of select="cell" /> 

             </line> 

所以,请分享XSLT代码,你有

的一个问题是与totalWidth可变

<xsl:variable name="totalWidth" select="sum(preceding-sibling::cell/@cellWidth)"/> 

在这点你位于column元素,它只有column元素作为前面的兄弟元素。这确实应该这样..

<xsl:variable name="left" select="sum(preceding-sibling::columns/cell/@cellWidth)"/> 

为了得到电池顶部t你从以前的行需要的值,所以你可以第一个(在xsl:for-each像这样前)定义一个变量

<xsl:variable name="previousRows" select="preceding-sibling::rows" /> 

然后是,则当前

<xsl:variable name="top" select="sum($previousRows/columns[@No = current()/@No]/cell/@cellHeight)"/> 

把此一并给出了这样的具有相同No计数从以前的行的列的情况。 ..

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="rows"> 
     <xsl:variable name="previousRows" select="preceding-sibling::rows" /> 
     <xsl:for-each select="columns"> 
      <xsl:variable name="left" select="sum(preceding-sibling::columns/cell/@cellWidth)"/> 
      <xsl:variable name="top" select="sum($previousRows/columns[@No = current()/@No]/cell/@cellHeight)"/> 
      <line l="{$left}" r="{$left + cell/@cellWidth - 1}" t="{$top}" h="{$top + cell/@cellHeight}"> 
       <xsl:value-of select="cell" /> 
      </line> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="/*"> 
     <sheet> 
      <xsl:apply-templates select="Cells/rows" /> 
     </sheet> 
    </xsl:template> 
</xsl:stylesheet> 

请注意,这并不完全给你期望的输出,因为你给的第一线为<line l="0" r="152" t="0" b="20">,但如果你的左边的位置有一个从零开始的索引中,“152”的宽度会给右值为151.

请注意使用Attribute Value Templates来简化属性的创建。

+0

谢谢让我检查,因为我是新的XSLT –