Xml 未知命名元素上的键

Xml 未知命名元素上的键,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我的XML结构如下所示: <Rootname> <FIRST a="1" otherhattrs="whatever"> </FIRST> <GROUPB a="b2" b="12" c="b456"> <SET NR="01"> <ENTRY type="10" otherhattr="whatever"/> </SET> <SET NR="02">

我的XML结构如下所示:

<Rootname>  
<FIRST a="1" otherhattrs="whatever">  
</FIRST>
<GROUPB a="b2" b="12" c="b456">
    <SET NR="01">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="20" otherhattr="whatever"/>
    </SET>
</GROUPB>
<GROUPC a="c3" b="23">
    <SET NR="01">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="30" otherhattr="whatever"/>
        <ENTRY type="50" otherhattr="whatever"/>
    </SET>
</GROUPC>
<GROUPD a="d4" b="34">
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="03">
        <ENTRY type="50" otherhattr="whatever"/>
        <ENTRY type="50" otherhattr="whatever"/>
        <ENTRY type="60" otherhattr="whatever"/>
    </SET>
</GROUPD>
</Rootname>
逻辑有点复杂,但我将描述: 有不同的组(组),它们都首先属于元素。所以首先我需要它们的属性“a”(第一个和组)和组的属性“b”。第二步是我需要集合的编号(NR)。第三步也是最后一步是我需要条目的“类型”和同一集合中相同类型的条目(条目)的数量

现在有一个例外: GROUPB是一个特殊的属性,如果有一个属性“c”,我不需要“b”,而需要“c”

我原以为我可以通过一个{GROUP attribute“a”、set attribute“NR”和ENTRY attribute“type”}的键来解决我的主要问题(同一集中具有相同类型的条目的数量),但我很沮丧,没有得到任何键或任何东西

我上一次使用XSL是这样的,问题是它在其他组和集合中对条目进行排序:

<xsl:output method="text" encoding="ISO-8859-15" indent="no"/>

<xsl:template match="/">

    <xsl:variable name="a" select="/*/FIRST/@a"/>

    <xsl:if test="string($a) != ''">
        <xsl:for-each select="/*/GROUPC[string(@a) != ''] | /*/GROUPB[string(@a) != '']| /*/GROUPD[string(@a) != '']">
            <xsl:sort select="@a"/>
            <xsl:variable name="out">
                <xsl:apply-templates select=".">
                    <xsl:with-param name="a" select="$a"/>
                </xsl:apply-templates>
            </xsl:variable>
            <xsl:value-of select="$out"/>
        </xsl:for-each>
    </xsl:if>
</xsl:template>    

<xsl:template match="GROUPB">
    <xsl:param name="a"/>

    <xsl:variable name="begin">
        <xsl:choose>
            <xsl:when test="@c">
                <xsl:value-of select="concat($a, '|', @a,'|',@c,'|')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($a, '|', @a,'|',@b,'|')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="result">
        <xsl:apply-templates select="SET/ENTRY">
            <xsl:with-param name="begin" select="$begin"/>
        </xsl:apply-templates>
    </xsl:variable>
    <xsl:value-of select="$result" />
</xsl:template>    

<xsl:template match="GROUPC">
    <xsl:param name="a"/>

    <xsl:variable name="begin">
        <xsl:value-of select="concat($a, '|', @a,'|',@b,'|')"/>
    </xsl:variable>
    <xsl:variable name="result">
        <xsl:apply-templates select="SET/ENTRY">
            <xsl:with-param name="begin" select="$begin"/>
        </xsl:apply-templates>
    </xsl:variable>
    <xsl:value-of select="$result" />
</xsl:template>

<xsl:template match="GROUPD">
    <xsl:param name="a"/>

    <xsl:variable name="begin">
        <xsl:value-of select="concat($a, '|', @a,'|',@b,'|')"/>
    </xsl:variable>
    <xsl:variable name="result">
        <xsl:apply-templates select="SET/ENTRY">
            <xsl:with-param name="begin" select="$begin"/>
        </xsl:apply-templates>
    </xsl:variable>
    <xsl:value-of select="$result" />
</xsl:template>

<xsl:key name="art" match="ENTRY" use="@type"/>

<xsl:template match="SET">
    <xsl:param name="begin"/>
    <xsl:variable name="nummer">
        <xsl:value-of select="@NR"/>
    </xsl:variable>
    <xsl:for-each select="ENTRY[generate-id() = generate-id(key('art',@type))]">
            <xsl:value-of select="concat($begin, $nummer, '|', @type, '|', count(../ENTRY[@type=current()/@type]))" /><xsl:text>
</xsl:text>
    </xsl:for-each>
</xsl:template>

最后一句话,可能有没有想要的属性之一的组、集和条目,这些属性不应该出现在结果中


有人能帮我吗?

我被你的描述弄糊涂了,根本无法理解你的代码。我想你想做这样的事情:

<Rootname>  
<FIRST a="1" otherhattrs="whatever">  
</FIRST>
<GROUPB a="b2" b="12" c="b456">
    <SET NR="01">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="20" otherhattr="whatever"/>
    </SET>
</GROUPB>
<GROUPC a="c3" b="23">
    <SET NR="01">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
        <ENTRY type="30" otherhattr="whatever"/>
        <ENTRY type="50" otherhattr="whatever"/>
    </SET>
</GROUPC>
<GROUPD a="d4" b="34">
    <SET NR="02">
        <ENTRY type="10" otherhattr="whatever"/>
    </SET>
    <SET NR="03">
        <ENTRY type="50" otherhattr="whatever"/>
        <ENTRY type="50" otherhattr="whatever"/>
        <ENTRY type="60" otherhattr="whatever"/>
    </SET>
</GROUPD>
</Rootname>
XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:key name="k" match="ENTRY" use="concat(generate-id(..), '|', @type)"/>

<xsl:template match="/Rootname">
    <xsl:variable name="first-a" select="FIRST/@a" />
    <xsl:for-each select="*/SET/ENTRY[count(. | key('k', concat(generate-id(..), '|', @type))[1]) = 1]">
        <xsl:value-of select="$first-a"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="../../@a"/>
        <xsl:text>|</xsl:text>
        <xsl:choose>
            <xsl:when test="../../@c">
                <xsl:value-of select="../../@c"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="../../@b"/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="../@NR"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="@type"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="count(key('k', concat(generate-id(..), '|', @type)))"/>       
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

|
|
|
|
|



嗨,我一直在困惑自己,但你的解决方案比我的好得多。所以我这边+1!这是一个很好的关键用法示例!一个问题:你能给我解释一下
计数(.|键)吗(…
如何解释点管道键?Thx!@Shnugo这是Muenchian分组的另一种形式-请看:Thx,我刚刚读了这篇文章,希望我得到了它…Muenchian分组的精彩解释。添加到我的链接列表中…哇,非常感谢michael,这是我正在寻找的xsl