每个复杂条件的XSLT转换

时间:2017-03-01 12:05:22

标签: html xml xslt foreach

我想转换这个xml

XML

<Page>
  <Sections>
    <Section id="parent_hdr_sec" rows="2" columns="1">
      <Sections>
        <Section id="plan_hdr" rows="0" columns="0" />
        <Section id="plan_hdr_dtl" rows="0" columns="0" />
      </Sections>
    </Section>
    <Section id="splitter_sec" rows="1" columns="2">
      <Sections>
        <Section id="parent_left_sec" rows="4" columns="1">
        </Section>
        <Section id="parent_right_sec" rows="7" columns="1">
        </Section>
      </Sections>
    </Section>
  </Sections>
</Page>

XSLT

<xsl:template match="/">
    <html>
      <body>
        <table>
          <xsl:for-each select="Page/Sections/Section">
            <tr>
              <td>
                <xsl:attribute name="id">
                  <xsl:value-of select="@id"/>
                </xsl:attribute>
                <table>
                  <xsl:variable name="Rows" select="@rows"/>
                  <xsl:variable name="Columns" select="@columns"/>

                  <xsl:for-each select="(//Section/Sections/Section)[$Rows >= position()]">
                    <tr>
                      <xsl:for-each select="(//Section/Sections/Section)[$Columns >= position()]">
                        <td>
                          <xsl:attribute name="id">
                            <xsl:value-of select="@id"/>
                          </xsl:attribute>
                        </td>
                      </xsl:for-each>
                    </tr>
                  </xsl:for-each>

                </table>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

我的输出

<html>
  <body>
    <table>
      <tr>
        <td id="parent_hdr_sec">
          <table>
            <tr>
              <td id="plan_hdr"></td>
            </tr>
            <tr>
              <td id="plan_hdr"></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td id="splitter_sec">
          <table>
            <tr>
              <td id="plan_hdr"></td>
              <td id="plan_hdr_dtl"></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

正确输出

&#39; ID&#39;我的输出中错误地更新了变量值。

我需要这样的输出,

<html>
  <body>
    <table>
      <tr>
        <td id="parent_hdr_sec">
          <table>
            <tr>
              <td id="plan_hdr"></td>
            </tr>
            <tr>
              <td id="plan_hdr_dtl"></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td id="splitter_sec">
          <table>
            <tr>
              <td id="parent_left_sec"></td>
              <td id="parent_right_sec"></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

我希望每个条件的XSLT都能获得上述输出。

任何人都可以帮我解决这个问题..

2 个答案:

答案 0 :(得分:0)

我真的不了解您使用$Rows$Columns变量尝试完成的任务。有一点似乎很清楚,在每个分支中,您只想处理属于该分支的节点 - 因此您应该以{{1}启动选择表达式}}

尝试一下:

//

请注意:

<xsl:template match="/">
    <html>
      <body>
        <table>
          <xsl:for-each select="Page/Sections/Section">
            <tr>
              <td>
                <xsl:attribute name="id">
                  <xsl:value-of select="@id"/>
                </xsl:attribute>
                <table>
                  <xsl:variable name="Rows" select="@rows"/>
                  <xsl:variable name="Columns" select="@columns"/>

                  <xsl:for-each select="Sections[$Rows >= position()]">
                    <tr>
                      <xsl:for-each select="Section[$Columns >= position()]">
                        <td>
                          <xsl:attribute name="id">
                            <xsl:value-of select="@id"/>
                          </xsl:attribute>
                        </td>
                      </xsl:for-each>
                    </tr>
                  </xsl:for-each>

                </table>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
</xsl:template>

可以写成:

<td>
    <xsl:attribute name="id">
        <xsl:value-of select="@id"/>
    </xsl:attribute>
    <!-- more -->
</td>

请参阅:https://www.w3.org/TR/xslt/#attribute-value-templates

答案 1 :(得分:0)

@Michael抱歉..发表评论部分....☺