0

我有这样的 XML

<root>
  <Users groupid="1">
    <User id="person1" name="ABC"  parentid="person7"/>
    <User id="person2" name="xyz"  parentid="person1"/>
    <User id="person3" name="LMN"  parentid="bac3"/>
    <User id="person4" name="PQR"  parentid="person2"/>
    <User id="person5" name="PQR" parentid="person1"/>
    <User id="person6" name="PQR" parentid="person7"/>
  </Users>
  <Users groupid="3">
    <User id="person7" name="ABC"  parentid="person11"/>
    <User id="person8" name="xyz"  parentid="person1"/>
    <User id="person3" name="LMN"  parentid="bac3"/>
    <User id="person4" name="PQR"  parentid="person2"/>
    <User id="person5" name="PQR" parentid="person1"/>
    <User id="person6" name="PQR" parentid="person7"/>
  </Users>
</root>

我需要使用xslt 1.0将其转换为=>

<Users groupid="1">
  <User id="person1" name="ABC"  parentid="person7" haschildinGroup="yes"/>
      <User id="person2" name="xyz"  parentid="person1" haschildinGroup="yes"/>
          <User id="person4" name="PQR"  parentid="person2" haschildinGroup="no"/>
      <User id="person5" name="PQR" parentid="person1" haschildinGroup="no"/>
  <User id="person3" name="LMN"  parentid="bac3" haschildinGroup="no"/> 
  <User id="person6" name="PQR" parentid="person7" haschildinGroup="no"/>
</Users>
<Users groupid="3">
  <User id="person7" name="ABC"  parentid="person11" haschildinGroup="yes"/>
      <User id="person6" name="PQR" parentid="person7" haschildinGroup="no"/>
  <User id="person8" name="xyz"  parentid="person1" haschildinGroup="no"/>
  <User id="person3" name="LMN"  parentid="bac3" haschildinGroup="yes"/>
      <User id="person5" name="PQR" parentid="person3"  haschildinGroup="no"/>
  <User id="person4" name="PQR"  parentid="person2"  haschildinGroup="no"/>
</Users>

指南: 1)我需要根据共享 parentid 及以上的人对值进行分组输出 xml 解释更多

2) 需要将 xml 缩进类似父子关系的缩进(在第一个添加额外的空格)用于 HTML 视图

我尝试过这样的示例,但其中的关键在整个 XML 上而不是在当前节点内

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="no"/>

      <xsl:key name="groups" match="User" use="@parentid" />
      <xsl:variable name="Space">&amp;nbsp;</xsl:variable>
      <xsl:variable name="addSpace" select="concat($Space,$Space)"/>

      <xsl:template match="/">
        <html>
          <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <xsl:for-each select="root/Users/User[generate-id(.)=generate-id(key('groups',@parentid)[1])]">
              <xsl:call-template name="selectusers"></xsl:call-template>
            </xsl:for-each>
          </table>
        </html>
      </xsl:template>

      <xsl:template name="selectusers">
          <xsl:for-each select="key('groups', @parentid)">
            <tr>
               <xsl:value-of select="$addSpace" disable-output-escaping="yes"/><!--Works for only 1st Level need to add If-->
              <td>

                <xsl:value-of select="@id"/>
              </td>
              <td>
                <!--HasChild =:(-->
              </td>
            </tr>
          </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>
4

1 回答 1

1

您的 XSLT 是为生成 HTML 而不是您所描述的所需输出而编写的,但我会假设您想要的实际上是您试图生成的 HTML。如果是这样,应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="no"/>

  <xsl:key name="groups" match="User" use="concat(../@groupid, '+', @parentid)" />
  <xsl:variable name="Space">&#xA0;</xsl:variable>
  <xsl:variable name="addSpace" select="concat($Space,$Space)"/>

  <xsl:template match="/">
    <html>
      <table width="100%" cellspacing="0" cellpadding="0" border="0">
        <tr>
          <th>ID</th>
          <th>Has Children</th>
        </tr>
        <xsl:apply-templates 
              select="root/Users/User[not(@parentid = ../User/@id)]" />
      </table>
    </html>
  </xsl:template>

  <xsl:template match="User">
    <xsl:variable name="children"
                  select="key('groups', concat(../@groupid, '+', @id))" />

    <tr>
      <td>
        <xsl:apply-templates select="." mode="spacing" />
        <xsl:value-of select="@id"/>
      </td>
      <td>
        <xsl:value-of select="substring('No Yes', 1 + 3 * boolean($children), 3)"/>
      </td>
    </tr>
    <xsl:apply-templates select="$children" />
  </xsl:template>

  <xsl:template match="User" mode="spacing">
    <xsl:value-of select="$addSpace" />
    <xsl:apply-templates 
          select="../User[@id = current()/@parentid]" mode="spacing" />
  </xsl:template>
</xsl:stylesheet>

当这在您的示例输入上运行时,结果是:

<html>
  <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <tr>
      <th>ID</th>
      <th>Has Children</th>
    </tr>
    <tr>
      <td>  person1</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td>    person2</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td>      person4</td>
      <td>No </td>
    </tr>
    <tr>
      <td>    person5</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person3</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person6</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person7</td>
      <td>Yes</td>
    </tr>
    <tr>
      <td>    person6</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person8</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person3</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person4</td>
      <td>No </td>
    </tr>
    <tr>
      <td>  person5</td>
      <td>No </td>
    </tr>
  </table>
</html>
于 2013-04-15T06:23:12.380 回答