XSLT:如何在<ol>中放置多个级别节点来代替xls:number格式

时间:2017-08-09 13:12:25

标签: xslt xslt-1.0

我是xsl的新手并且正在弄乱一个示例模板来尝试解决问题。 我设法达到这个目标,如下面的片段。不确定我为不同的System.AreaPath应用模板的方式是否正确,但似乎有用吗?

我现在正在努力的一件事就是使用ol(或者用于Epic,ol用于所有兄弟姐妹)和css,而不是模板样本将每个放在div和h {$ level}中。

我希望这也能解决数字格式的当前问题 - 应用模板在应用两次时不会重置计数。

任何建议或样品都会受到赞赏,因为我没有找到 - 或者更确切地说是如何正确理解 - 我找到的各种搜索结果和样本。 (for-each和经常出现的时候)

我希望上面的内容有道理并且粘贴的代码足够了吗? 提前致谢!   - 雅克

xml :(编辑:xml,而不是xlm)

<result executedDate="8/8/2017" executedBy="abc" email="abc@email.com">
<columns>
<System.Id name="ID" width="75" type="Integer"/>
<System.WorkItemType name="Work Item Type" width="75" type="String"/>
<System.Title name="Title" width="150" type="String"/>
<System.AreaPath name="Area Path" width="75" type="TreePath"/>
</columns>
<options>
 ...
</options>
<workitem id="1051" type="Epic" state="In Progress">
<System.Id>1051</System.Id>
<System.WorkItemType>Epic</System.WorkItemType>
<System.Title>Epic1</System.Title>
<System.AreaPath>Path1</System.AreaPath>
<workitem id="5411" type="Feature" state="Done">
  <System.Id>5411</System.Id>
  <System.WorkItemType>Feature</System.WorkItemType>
  <System.Title>Feature1</System.Title>
  <System.AreaPath>Path1</System.AreaPath>
  <workitem id="5414" type="Product Backlog Item" state="Done">
    <System.Id>5414</System.Id>
    <System.WorkItemType>Product Backlog Item</System.WorkItemType>
    <System.Title>Backlog Item 1</System.Title>
    <System.AreaPath>Path1</System.AreaPath>
  </workitem>
  <workitem id="5418" type="Product Backlog Item" state="Done">
    <System.Id>5418</System.Id>
    <System.WorkItemType>Product Backlog Item</System.WorkItemType>
    <System.Title>Backlog Item 2</System.Title>
    <System.AreaPath>Path1</System.AreaPath>
  </workitem>
 </workitem>
</workitem>
<workitem id="1529" type="Epic" state="In Progress">
<System.Id>1529</System.Id>
<System.WorkItemType>Epic</System.WorkItemType>
<System.Title>Epic 2</System.Title>
<System.AreaPath>Path2</System.AreaPath>
<workitem id="3956" type="Feature" state="Done">
  <System.Id>3956</System.Id>
  <System.WorkItemType>Feature</System.WorkItemType>
  <System.Title>Feature 2</System.Title>
  <System.AreaPath>Path 2</System.AreaPath>
  <workitem id="5955" type="Product Backlog Item" state="Done">
    <System.Id>5955</System.Id>
    <System.WorkItemType>Product Backlog Item</System.WorkItemType>
    <System.Title>Backlog item 3</System.Title>
    <System.AreaPath>Path2</System.AreaPath>
  </workitem>
  <workitem id="6667" type="Product Backlog Item" state="Done">
    <System.Id>6667</System.Id>
    <System.WorkItemType>Product Backlog Item</System.WorkItemType>
    <System.Title>Backlog item 4</System.Title>
    <System.AreaPath>Path2</System.AreaPath>
  </workitem>
</workitem>

的xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
    <html>
        <head>
            <meta charset="UTF-8"/>
        </head>
        <body>
            <h2>Section 1</h2>
            <xsl:apply-templates select="/result /workitem[@type != 'Bug' and System.AreaPath='Path2']"/>
            <p/>
            <h2>Section 2</h2>
            <xsl:apply-templates select="/result /workitem[@type != 'Bug' and System.AreaPath='Path1']"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="//workitem">
    <xsl:variable name="level" select="count(ancestor-or-self::*) "/>
    <xsl:element name="h{$level}">
        <xsl:number level="multiple" format="1. "/>
        <xsl:value-of select="System.Title"/>
    </xsl:element>
    <div style="margin-left:15px;">
        <xsl:apply-templates select="workitem"/>
    </div>
</xsl:template>

html输出:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<meta charset="UTF-8">
</head>
<body>
   <h2>Section 1</h2>
   <h2>2. Epic 2</h2>
   <div style="margin-left:15px;">
     <h3>2.1. Feature 2</h3>
       <div style="margin-left:15px;">
       <h4>2.1.1. Backlog item 3</h4>
       <div style="margin-left:15px;"></div>
       <h4>2.1.2. Backlog item 4</h4>
       <div style="margin-left:15px;"></div>
   </div>
 </div>
<p></p>
<h2>Section 2</h2>
<h2>1. Epic1</h2>
<div style="margin-left:15px;">
  <h3>1.1. Feature1</h3>
  <div style="margin-left:15px;">
    <h4>1.1.1. Backlog item 1</h4>
    <div style="margin-left:15px;"></div>
    <h4>1.1.2. Backlog item 2</h4>
    <div style="margin-left:15px;"></div>
  </div>
</div>

所需的html输出: 这是基于我正在尝试复制的当前使用视图。可能以后选择不使用该表,但<ul>布局是我之后的。

此外,&#39;史诗&#39;可能不会使用节点,并且可以在生成xml的查询中排除该节点

<table>
    <tr>
        <td class=class1>Section 1</td> <!-- list all features from Path2 -->
    </tr>
    <tr>
        <td>
            <ul class=class2>
                <li>Feature 3
                    <ul>
                        <li>Backlog item 5</li>
                        <li>Backlog item 6</li>
                    </ul>
                </li>
                <li>Feature 4
                    <ul>
                        <li>Backlog item 7</li>
                        <li>Backlog item 8</li>
                    </ul>
                </li>
            </ul>
        </td>
    </tr>
    <tr>
        <td class=class1>Section 2</td>  <!-- list all features from Path1 -->
    </tr>
    <tr>
        <td>
            <ul class=class2>
                <li>Feature1
                    <ul>
                        <li>Backlog item 1</li>
                        <li>Backlog item 2</li>
                    </ul>
                </li>
                <li>Feature 2
                    <ul>
                        <li>Backlog item 3</li>
                        <li>Backlog item 4</li>
                    </ul>
                </li>
            </ul>
        </td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

您的预期输出不明确。如果你想要一个嵌套列表,你可以做到:

XSLT 1.0

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

<xsl:template match="/result">
    <html>
        <body>
            <ul>
                <xsl:apply-templates select="workitem[@type != 'Bug' and starts-with(System.AreaPath, 'Path')]"/>
            </ul>
        </body>
    </html>
</xsl:template>

<xsl:template match="workitem">
    <li>
        <xsl:number level="multiple" format="1. "/>
        <xsl:value-of select="System.Title"/>
    </li>
    <xsl:if test="workitem">
        <ul>
            <xsl:apply-templates select="workitem"/>
        </ul>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

并获得:

<强>结果

<html>
  <body>
    <ul>
      <li>1. Epic1</li>
      <ul>
        <li>1.1. Feature1</li>
        <ul>
          <li>1.1.1. Backlog Item 1</li>
          <li>1.1.2. Backlog Item 2</li>
        </ul>
      </ul>
      <li>2. Epic 2</li>
      <ul>
        <li>2.1. Feature 2</li>
        <ul>
          <li>2.1.1. Backlog item 3</li>
          <li>2.1.2. Backlog item 4</li>
        </ul>
      </ul>
    </ul>
  </body>
</html>