【问题标题】:XSLT Transformation For Each Complex Condition每个复杂条件的 XSLT 转换
【发布时间】:2017-07-20 18:04:40
【问题描述】:

我想转换这个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>

正确输出

我的输出中错误地更新了“id”变量值。

我需要像下面这样的输出,

<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 都能获得上述输出。

谁能帮我解决这个问题..

【问题讨论】:

    标签: html xml xslt foreach


    【解决方案1】:

    我真的不明白你想用$Rows$Columns 变量来完成什么。一件似乎很清楚的事情是,在每个分支中,您只想处理属于该分支的节点 - 所以您应该// 开始您的 select 表达式.

    不妨试试:

    <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>
    

    可以简写为:

    <td id="{@id}">
        <!-- more -->
    </td>
    

    见:https://www.w3.org/TR/xslt/#attribute-value-templates

    【讨论】:

    • 感谢您的回复。如果我使用上面的,我会得到这样的输出
    • @Hari 请不要在 cmets 中发布代码,它非常不可读。
    【解决方案2】:

    @Michael 对不起..在 cmets 部分发帖....☺

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多