Html xsl样式表:获取具有相同属性值的父元素的子元素

Html xsl样式表:获取具有相同属性值的父元素的子元素,html,xml,xslt,Html,Xml,Xslt,我正在开发一个JavaFX应用程序,我有一个xml文件要转换成html。xml文件中的某些元素具有相同的属性名称,但子名称不同。我想根据具有相同属性值的父元素对子元素进行分组 xml文件如下所示: 模型结构 模型数据 自由的 独立的 没有一个 onsdn 模型结构 糟糕的数据 自由的 独立的 GLC nsisodsd 这里有两个选项。第一个选项将同时适用于1.0和2.0处理器。第二个选项仅适用于2.0处理器 XSLT1.0/2.0(示例:) XSLT2.0(示例:) 您能使

我正在开发一个JavaFX应用程序,我有一个xml文件要转换成html。xml文件中的某些元素具有相同的属性名称,但子名称不同。我想根据具有相同属性值的父元素对子元素进行分组

xml文件如下所示:


模型结构
模型数据
自由的
独立的
没有一个
onsdn
模型结构
糟糕的数据
自由的
独立的
GLC
nsisodsd

这里有两个选项。第一个选项将同时适用于1.0和2.0处理器。第二个选项仅适用于2.0处理器

XSLT1.0/2.0(示例:)


  • XSLT2.0(示例:)

    
    

  • 您能使用XSLT 2.0吗?--另外,一个不止一组的例子会很有用。我编辑了结果,我想展示两组。我尝试将元素与临时变量一起使用,但xsl不允许更改变量的值。您理解使用XSLT2.0的问题吗?这很了不起!我花了一些时间试图理解这个过程。非常感谢您的分享!!
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
      <xsl:strip-space elements="*"/>
    
      <!--Group workflows by name attribute.-->
      <xsl:key name="workflows" match="workflow" use="@name"/>
    
      <xsl:template match="/*">
        <html>
          <body>
            <!--First workflow in the group.-->
            <xsl:for-each select="workflow[generate-id()=generate-id(key('workflows',@name))]">
              <h3><xsl:value-of select="@name"/></h3>
              <ol type="1">
                <!--All of the workflows in the group.-->
                <xsl:apply-templates select="key('workflows',@name)"/>
              </ol>
            </xsl:for-each>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="workflow">
        <li>
          <xsl:apply-templates select="*[not(self::name)]"/>
        </li>
      </xsl:template>
    
      <xsl:template match="workflow/*">
        <!--Get the first character of the element name and make it uppercase.-->
        <xsl:variable name="ic" 
          select="translate(substring(name(),1,1),
          'abcdefghijklmnopqrstuvwxyz',
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
        <p><xsl:value-of select="concat($ic,substring(name(),2),': ',.)"/></p>
      </xsl:template>
    
    </xsl:stylesheet>
    
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="/*">
        <html>
          <body>
            <!--First workflow in the group.-->
            <xsl:for-each-group select="workflow" group-by="@name">
              <h3><xsl:value-of select="current-grouping-key()"/></h3>
              <ol type="1">
                <!--All of the workflows in the group.-->
                <xsl:apply-templates select="current-group()"/>
              </ol>
            </xsl:for-each-group>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="workflow">
        <li>
          <xsl:apply-templates select="* except name"/>
        </li>
      </xsl:template>
    
      <xsl:template match="workflow/*">
        <!--Get the first character of the element name and make it uppercase.-->
        <xsl:variable name="name" 
          select="replace(name(),'^.',upper-case(substring(name(),1,1)))"/>
        <p><xsl:value-of select="concat($name,': ',.)"/></p>
      </xsl:template>
    
    
    </xsl:stylesheet>