css - 如何在 XSLT 上动态设置边距

标签 css xml xslt

我正在尝试使用 XSLT 和 css 实现 xml 文件的 TreeView 。 我在 xml 中嵌套了子项,并遍历它们以显示每个条目的内容。

每个 child 都是一个带有消息条目的记录器。我想根据我正确获得的祖先计数缩进每个子表。但是尝试动态设置表格边距 margin-left="$margin*10"不起作用。

这是我的 XSLT/xml

<xsl:template match="operationLogger">
    <xsl:variable name="margin" select="count(ancestor::*) * 10"/>
      <table class="normalTable" cols="4">
        <xsl:attribute name="margin-left">
          <xsl:value-of select="$margin"/>
        </xsl:attribute>
        <tr class="errorsCollectionTitle">
          <td colspan="4">
            <xsl:value-of select="@name"/>
          </td>
        </tr>
        <xsl:if test="operationLogEntry">
          <xsl:apply-templates select="operationLogEntry"/>
        </xsl:if>
        <xsl:if test="operationLogger">
          <xsl:apply-templates select="operationLogger"/>
        </xsl:if>
      </table>
  </xsl:template>

  <xsl:template match="operationLogEntry">
    <tr>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="position() mod 2 = 1">rowData</xsl:when>
          <xsl:otherwise>rowAlternatingData</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <td>
        <xsl:attribute name="class">
          <xsl:choose>
            <xsl:when test="@level = 'Error'">errorImage</xsl:when>
            <xsl:when test="@level = 'Warning'">warningImage</xsl:when>
            <xsl:when test="@level = 'Info'">informationImage</xsl:when>
            <xsl:when test="@level = 'Debug'">informationImage</xsl:when>
          </xsl:choose>
        </xsl:attribute>
      </td>
      <td class="timeStamp">
        <xsl:value-of select="@timeStamp"/>
      </td>
      <td class="source">
        <xsl:value-of select="../loggerSource/@computer" />
        /
        <xsl:value-of select="../loggerSource/@user" />
      </td>
      <td class="message">
          <div>
        <xsl:attribute name="class">
            <xsl:choose>
              <xsl:when test="@level = 'Error'">errorColor</xsl:when>
              <xsl:when test="@level = 'Warning'">warningColor</xsl:when>
              <xsl:when test="@level = 'Info'">informationColor</xsl:when>
              <xsl:when test="@level = 'Debug'">informationColor</xsl:when>
            </xsl:choose>
          </xsl:attribute>
          <xsl:value-of select="@message"/>
        </div>
      </td>
    </tr>
  </xsl:template>
<operationLogger name="">
      <loggerSource domain="user" computer="computer" user="account" />
      <operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Adding 1 to transactions list" />
      <operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Executing 1 transactions..." />
      <operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Executed 1 transactions successfully!" />
      <operationLogger name="TransactionOperationCollection.Execute()">
        <loggerSource domain="user" computer="computer" user="account" />
        <operationLogEntry timeStamp="2015/02/16 07:15:21" level="Info" message="Commiting transaction 0:Transaction" />
        <operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Committed transaction 0:Transaction" />
        <operationLogger name="TransactionOperation.Commit()">
        <operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Setting Auditonly" />
          <operationLogEntry timeStamp="2015/02/16 07:15:22" level="Info" message="Succesfully changed Audit Only" />
        </operationLogger>
      </operationLogger>
    </operationLogger>

我尝试从 1 循环到“祖先数量”并添加 span 或 div。我尝试推送第一行的内容。似乎没有什么对我有用。

如有任何帮助,我们将不胜感激!

最佳答案

没有margin-left这样的东西属性。您可以使用 style不过,此属性:

<xsl:attribute name="style">
    <xsl:value-of select="concat('margin-left: ', $margin, 'px;')"/>
</xsl:attribute>

但是使用内联样式很少是一件好事。我建议您将表放在 ul/li 层次结构中。这样,您可以将页边距放在 CSS 文件/<style> 中。部分而不是动态计算它们:

<xsl:template match="/">
  <ul>
    <xsl:apply-templates />
  </ul>
</xsl:template>     

<xsl:template match="operationLogger">
  <li>
    <table class="normalTable" cols="4">
      <tr class="errorsCollectionTitle">
        <td colspan="4">
          <xsl:value-of select="@name"/>
        </td>
      </tr>
      <xsl:apply-templates select="operationLogEntry"/>
    </table>
    <xsl:if test="operationLogger">
      <ul>
        <xsl:apply-templates select="operationLogger"/>
      </ul>
    </xsl:if>
  </li>
</xsl:template>

关于css - 如何在 XSLT 上动态设置边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040040/

相关文章:

ios - RestKit 0.20.1 如何映射父 ID

java - 如何通过电子邮件将 xslt 报告发送给 xls 文件中给出的用户列表?

XSLT 1.0 累加、求和、乘法

html - Chrome 上的 div 排列错误

jquery - 在不属于 DOM 的对象数组上使用 jQuery 选择器

mysql - Xml To MYSQL 导入使用mysql过程

XSLT 副本 - 属性留空

javascript - 分页.js : How can I set up an additional page counter for only a few pages

css - Internet Explorer 8,向左浮动删除背景色

javascript - HTML - XML - 跳转到另一个选项卡/页面元素