当前位置: 首页 > 知识库问答 >
问题:

XSL-基于元素属性的组

葛霄
2023-03-14

我如何使用“insref”和“pref”对下面的元素进行分组,我在xslt版本1中使用了生成密钥id。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
   <mylist>
    <cd id="1" insref="#10">
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd id ="2" insref="#20" pref="#1">
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd id="3" insref="#520" pref="#1">
        <title>Lonly heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd id="4" insref="#56" pref="#1">
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd id="5" insref="#56" pref="#2">
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd id="6" insref="#56" pref="#2">
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd id="7" insref="#56" pref="#2">
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd id="8" insref="#78" pref="#2">
        <title>Maggie May</title>
        <artist>Rod Stewart</artist>
        <country>UK</country>
        <company>Pickwick</company>
        <price>8.50</price>
        <year>1990</year>
    </cd>
    <cd id="9" insref="#45" pref="#1">
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>

    <cd id="10" insref="#45" pref="#2">
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>
 </mylist>  
</catalog>

期望的结果:

TITLE               ARTIST          ID  INSREF  PREF    Qty
Empire Burlesque    Bob Dylan       1   10      
Hide your heart     Bonnie Tyler    2   20      1       1   
Lonly heart         Bonnie Tyler    3   520     1       1       
Still got the blues Gary Moore      4   56      1       1   
Still got the blues Gary Moore      4   56      2       3   
Maggie May          Rod Stewart     8   78      2       1   
Romanza             Andrea Bocelli  9   45      1       1
Romanza             Andrea Bocelli  10  45      2       1

这是我所拥有的。我也在使用密钥生成函数。任何想法这个xslt有什么问题。至于计数,我正在考虑获得唯一的密钥计数。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="by-accessRefs" match="cd" use="concat(@insref,@pref)"/>

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
        <th>INSREF</th>
         <th>PREF</th>
      </tr>
      <xsl:for-each select="catalog/mylist/cd[generate-id(.) = generate-id(key('by-accessRefs',concat(@insref,@pref)[1])]">

         <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
        <td><xsl:value-of select="@insref"/></td>
        <td><xsl:value-of select="@pref"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

共有2个答案

孟成文
2023-03-14

我假设您需要的输出是一个HTML表:

样式表文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd"
    version="2.0">

    <xsl:output method="html" indent="yes"/>

    <xsl:key name="kTitle" match="title" use="text()"/>

    <xsl:template match="/">
        <table>
            <thead>
                <tr>
                    <th>TITLE</th>
                    <th>ARTIST</th>
                    <th>ID</th>
                    <th>INSREF</th>
                    <th>PREF</th>
                    <th>Qty</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each-group select="//cd" group-by="@id">
                    <tr>
                        <td><xsl:value-of select="title"/></td>
                        <td><xsl:value-of select="artist"/></td>
                        <td><xsl:value-of select="@id"/></td>
                        <td><xsl:value-of select="substring-after(@insref,'#')"/></td>
                        <td><xsl:value-of select="substring-after(@pref,'#')"/></td>
                        <td><xsl:value-of select="count(key('kTitle',title/text()))"/></td>
                    </tr>
                </xsl:for-each-group>
            </tbody>
        </table>
    </xsl:template>

</xsl:stylesheet>

结果文件:

<table>
   <thead>
      <tr>
         <th>TITLE</th>
         <th>ARTIST</th>
         <th>ID</th>
         <th>INSREF</th>
         <th>PREF</th>
         <th>Qty</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Empire Burlesque</td>
         <td>Bob Dylan</td>
         <td>1</td>
         <td>10</td>
         <td></td>
         <td>1</td>
      </tr>
      <tr>
         <td>Hide your heart</td>
         <td>Bonnie Tyler</td>
         <td>2</td>
         <td>20</td>
         <td>1</td>
         <td>1</td>
      </tr>
      <tr>
         <td>Lonly heart</td>
         <td>Bonnie Tyler</td>
         <td>3</td>
         <td>520</td>
         <td>1</td>
         <td>1</td>
      </tr>
      <tr>
         <td>Still got the blues</td>
         <td>Gary Moore</td>
         <td>4</td>
         <td>56</td>
         <td>1</td>
         <td>4</td>
      </tr>
      <tr>
         <td>Still got the blues</td>
         <td>Gary Moore</td>
         <td>5</td>
         <td>56</td>
         <td>2</td>
         <td>4</td>
      </tr>
      <tr>
         <td>Still got the blues</td>
         <td>Gary Moore</td>
         <td>6</td>
         <td>56</td>
         <td>2</td>
         <td>4</td>
      </tr>
      <tr>
         <td>Still got the blues</td>
         <td>Gary Moore</td>
         <td>7</td>
         <td>56</td>
         <td>2</td>
         <td>4</td>
      </tr>
      <tr>
         <td>Maggie May</td>
         <td>Rod Stewart</td>
         <td>8</td>
         <td>78</td>
         <td>2</td>
         <td>1</td>
      </tr>
      <tr>
         <td>Romanza</td>
         <td>Andrea Bocelli</td>
         <td>9</td>
         <td>45</td>
         <td>1</td>
         <td>2</td>
      </tr>
      <tr>
         <td>Romanza</td>
         <td>Andrea Bocelli</td>
         <td>10</td>
         <td>45</td>
         <td>2</td>
         <td>2</td>
      </tr>
   </tbody>
</table>
桂和同
2023-03-14

XSLT 1.0转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kCDByProps" match="cd" use=
  "concat(title,'+',artist,'+',@insref,'+',@pref)"/>

 <xsl:template match="/*">
     <table>
       <thead>
         <tr>
           <td>TITLE</td><td>ARTIST</td><td>ID</td><td>INSREF</td><td>PREF</td><td>Qty</td>
         </tr>
       </thead>
       <xsl:apply-templates select=
        "mylist/cd
            [generate-id()
            =
             generate-id(key('kCDByProps', 
                               concat(title,'+',artist,'+',@insref,'+',@pref)
                              )[1])
            ]
        "/>
     </table>
 </xsl:template>

 <xsl:template match="cd">
  <xsl:variable name="vGroup" select=
    "key('kCDByProps', concat(title,'+',artist,'+',@insref,'+',@pref))"/>

  <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
    <td><xsl:value-of select="@id"/></td>
    <td><xsl:value-of select="substring(@insref, 2)"/></td>
    <td><xsl:value-of select="substring(@pref, 2)"/></td>
    <xsl:variable name="vCount" select=
           "count($vGroup[@pref = current()/@pref])"/>
    <td>
        <xsl:if test="$vCount"><xsl:value-of select="$vCount"/></xsl:if>
    </td>
  </tr>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<catalog>
    <mylist>
        <cd id="1" insref="#10">
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>USA</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
        </cd>
        <cd id ="2" insref="#20" pref="#1">
            <title>Hide your heart</title>
            <artist>Bonnie Tyler</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
        </cd>
        <cd id="3" insref="#520" pref="#1">
            <title>Lonly heart</title>
            <artist>Bonnie Tyler</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
        </cd>
        <cd id="4" insref="#56" pref="#1">
            <title>Still got the blues</title>
            <artist>Gary Moore</artist>
            <country>UK</country>
            <company>Virgin records</company>
            <price>10.20</price>
            <year>1990</year>
        </cd>
        <cd id="5" insref="#56" pref="#2">
            <title>Still got the blues</title>
            <artist>Gary Moore</artist>
            <country>UK</country>
            <company>Virgin records</company>
            <price>10.20</price>
            <year>1990</year>
        </cd>
        <cd id="6" insref="#56" pref="#2">
            <title>Still got the blues</title>
            <artist>Gary Moore</artist>
            <country>UK</country>
            <company>Virgin records</company>
            <price>10.20</price>
            <year>1990</year>
        </cd>
        <cd id="7" insref="#56" pref="#2">
            <title>Still got the blues</title>
            <artist>Gary Moore</artist>
            <country>UK</country>
            <company>Virgin records</company>
            <price>10.20</price>
            <year>1990</year>
        </cd>
        <cd id="8" insref="#78" pref="#2">
            <title>Maggie May</title>
            <artist>Rod Stewart</artist>
            <country>UK</country>
            <company>Pickwick</company>
            <price>8.50</price>
            <year>1990</year>
        </cd>
        <cd id="9" insref="#45" pref="#1">
            <title>Romanza</title>
            <artist>Andrea Bocelli</artist>
            <country>EU</country>
            <company>Polydor</company>
            <price>10.80</price>
            <year>1996</year>
        </cd>
        <cd id="10" insref="#45" pref="#2">
            <title>Romanza</title>
            <artist>Andrea Bocelli</artist>
            <country>EU</country>
            <company>Polydor</company>
            <price>10.80</price>
            <year>1996</year>
        </cd>
    </mylist>
</catalog>

生成以下HTML输出:

<table>
   <thead>
      <tr>
         <td>TITLE</td>
         <td>ARTIST</td>
         <td>ID</td>
         <td>INSREF</td>
         <td>PREF</td>
         <td>Qty</td>
      </tr>
   </thead>
   <tr>
      <td>Empire Burlesque</td>
      <td>Bob Dylan</td>
      <td>1</td>
      <td>10</td>
      <td/>
      <td/>
   </tr>
   <tr>
      <td>Hide your heart</td>
      <td>Bonnie Tyler</td>
      <td>2</td>
      <td>20</td>
      <td>1</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Lonly heart</td>
      <td>Bonnie Tyler</td>
      <td>3</td>
      <td>520</td>
      <td>1</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Still got the blues</td>
      <td>Gary Moore</td>
      <td>4</td>
      <td>56</td>
      <td>1</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Still got the blues</td>
      <td>Gary Moore</td>
      <td>5</td>
      <td>56</td>
      <td>2</td>
      <td>3</td>
   </tr>
   <tr>
      <td>Maggie May</td>
      <td>Rod Stewart</td>
      <td>8</td>
      <td>78</td>
      <td>2</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Romanza</td>
      <td>Andrea Bocelli</td>
      <td>9</td>
      <td>45</td>
      <td>1</td>
      <td>1</td>
   </tr>
   <tr>
      <td>Romanza</td>
      <td>Andrea Bocelli</td>
      <td>10</td>
      <td>45</td>
      <td>2</td>
      <td>1</td>
   </tr>
</table>

当在浏览器中查看时,这似乎是想要的正确结果:

 类似资料:
  • 我对xslt有这个问题。样式表中有一个名为misctable的表,它被放在一个变量miscTables中。例如在节点 id="_384PLATE"具有值A1、A2、B1。当我标记并将其与xml进行比较时,我需要保存R[@i=2]/C/@i属性的值。在这种情况下,我需要得到值2,3,26 xml xslt 非常感谢。

  • 我正在使用Jackson的JsonNode解析json。我的json结构如下所示(设为变量rawJson): 在Java中: 如何通过过滤属性<code>name</code>来获得特定的JsonNode元素?如果我想得到<code>值</code>大于X的JsonNode元素,怎么样?我是否可以在不遍历<code>结果</code>变量的情况下完成这些操作?

  • 在使用Vdt的过程中,你也许会遇到这样一个问题:设置的属性,并不能渲染出来,例如: <input maxlength="10" /> 上述例子中,设置的maxlength属性丢失了。要理解这个原因,可以看看这段介绍 Content attribute & IDL attribute, 以及Virtual-Dom文档中对VNode的介绍 Content vs IDL attribute Conte

  • 我正在尝试访问HTML代码中特定的元素。 定义元素的代码部分如下所示 。 在下面的第二行代码之后,我尝试声明表达式和,它们都返回java.lang.String类型的“oddrow cellcont” 但是,我尝试了下面的代码片段,它不能工作,因为循环中的条件返回false。我还尝试使用和、、、...以所有可能的组合使用if条件。 我肯定被困住了;我需要帮助。

  • 我有一个这样的xml结构。我想做一个模式(XSD)验证,使

  • 问题内容: 在检查Chrome Dev工具时,我注意到以下CSS片段: 可以在Chrome中完美运行(请参见下面的屏幕截图)。 这使我可以通过CSS定义标签的src属性。在Firefox中不起作用。到目前为止,我认为不可能通过CSS直接修改src属性,而且我还没有发现有人在谈论这一点。 那么,这仅仅是Chrome中的专有功能,还是Chrome正在实施W3C草案或其他我不知道的类似产品? 问题答案: