不同的元素和分组 [英] Distinct elements and grouping

查看:19
本文介绍了不同的元素和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下 xml 片段:

Given the following xml fragment:

<Problems>
  <Problem>
    <File>file1</File>
    <Description>desc1</Description>
  </Problem>
  <Problem>
    <File>file1</File>
    <Description>desc2</Description>
  </Problem>
  <Problem>
    <File>file2</File>
    <Description>desc1</Description>
  </Problem>
</Problems>

我需要制作类似的东西

<html>
  <body>
    <h1>file1</h1>
    <p>des1</p>
    <p>desc2</p>
    <h1>file2</h1>
    <p>des1</p>
  </body>
</html>

我尝试使用密钥,例如

<xsl:key name="files" match="Problem" use="File"/>

但我真的不明白如何将它带到下一步,或者这是否是正确的方法.

but I don't really understand how to take it to the next step, or if that's even the right approach.

推荐答案

这是我使用 Muenchean 方法的方法.谷歌xslt muenchean"从更聪明的人那里获得更多信息.可能有一个聪明的方法,但我会把它留给其他人.

Here's how I'd do it, using the Muenchean method. Google 'xslt muenchean' for more info from smarter people. There might be a clever way, but I'll leave that to others.

请注意,我避免在 xml 元素名称的开头使用大写字母,例如文件",但这取决于您.

One note, I avoid using capitals at the start of xml element names, eg 'File', but that's up to you.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:key name="files" match="/Problems/Problem/File" use="./text()"/>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="Problems"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Problems">
        <xsl:for-each select="Problem/File[generate-id(.) = generate-id(key('files', .))]">
            <xsl:sort select="."/>
            <h1>
                <xsl:value-of select="."/>
            </h1>
            <xsl:apply-templates select="../../Problem[File=current()/text()]"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="Problem">
        <p>
            <xsl:value-of select="Description/text()"/>
        </p>
    </xsl:template>
</xsl:stylesheet>

这个想法是,使用每个 File 元素的文本值来键入它.然后仅显示与键控元素相同的文件值.要检查它们是否相同,请使用 generate-id.有一种类似的方法,您可以比较第一个匹配的元素.我不能告诉你哪个更有效.

The idea is, key each File element using it's text value. Then only display the file values if they are the same element as the keyed one. To check if they're the same, use generate-id. There is a similar approach where you compare the first element that matches. I can't tell you which is more efficient.

我已经使用 Marrowsoft Xselerator 测试了这里的代码,我最喜欢的 xslt 工具,虽然不再可用,afaik.我得到的结果是:

I've tested the code here using Marrowsoft Xselerator, my favorite xslt tool, although no longer available, afaik. The result I got is:

<html>
<body>
<h1>file1</h1>
<p>desc1</p>
<p>desc2</p>
<h1>file2</h1>
<p>desc1</p>
</body>
</html>

这是使用 msxml4.

This is using msxml4.

我已按文件对输出进行排序.我不确定你是否想要那个.

I have sorted the output by File. I'm not sure if you wanted that.

我希望这会有所帮助.

这篇关于不同的元素和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆