0

我对 XSLT 非常陌生,需要一些帮助才能继续前进。我能够创建一个 XSL2.0 来仅获取错误的成功值并删除重复项,但是将 UNIQUE QUEID 值作为单个列表获取,如 output.xml 中所示,这是我想要的,但我得到了包括重复项在内的所有值(100,100,100,101,103)。任何帮助将不胜感激。

输入 XML

        <?xml version="1.0"?>
        <Entities>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>95</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>898</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>454</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>101</QUEID>
            <ENTY>45</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>102</QUEID>
            <ENTY>454</ENTY>
            <success>true</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>103</QUEID>
            <ENTY>56</ENTY>
            <success>true</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>103</QUEID>
            <ENTY>656</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>104</QUEID>
            <ENTY>234</ENTY>
            <success>true</success>
        </Entity>
        </Entities>

使用 XSL

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
            <xsl:strip-space elements="*"/>
            <xsl:template match="node()|@*">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="//Entity[success='true']"/>
            <xsl:template match="/*">
                <Entities>
                    <xsl:for-each-group select="Entity" group-by="QUEID">
                        <xsl:apply-templates select="."/>
                    </xsl:for-each-group>
                    <EntityList>
                        <xsl:value-of select="//Entity[success='false']/QUEID" separator=","/>
                    </EntityList>
                </Entities>
            </xsl:template>
        </xsl:stylesheet>

所需的输出 XML

        <?xml version="1.0" encoding="UTF-8"?>
        <Entities>
            <Entity>
                <Type/>
                <QUEID>100</QUEID>
                <ENTY>95</ENTY>
                <success>false</success>
            </Entity>
            <Entity>
                <Type/>
                <QUEID>101</QUEID>
                <ENTY>45</ENTY>
                <success>false</success>
            </Entity>
            <Entity>
                <Type/>
                <QUEID>103</QUEID>
                <ENTY>56</ENTY>
                <success>false</success>
            </Entity>
            <EntityList>100,101,103</EntityList>
        </Entities>
4

1 回答 1

0

这是我的建议:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
            <xsl:strip-space elements="*"/>
            <xsl:template match="node()|@*">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>
                </xsl:copy>
            </xsl:template>

            <xsl:template match="/*">
                <Entities>
                    <xsl:variable name="group-heads" as="element(Entity)*">
                      <xsl:for-each-group select="Entity[success = 'false']" group-by="QUEID">
                        <xsl:sequence select="."/>
                      </xsl:for-each-group>
                    </xsl:variable>
                    <xsl:apply-templates select="$group-heads"/>
                    <EntityList>
                        <xsl:value-of select="$group-heads/QUEID" separator=","/>
                    </EntityList>
                </Entities>
            </xsl:template>
        </xsl:stylesheet>

它转变

<?xml version="1.0"?>
        <Entities>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>95</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>898</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>100</QUEID>
            <ENTY>454</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>101</QUEID>
            <ENTY>45</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>102</QUEID>
            <ENTY>454</ENTY>
            <success>true</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>103</QUEID>
            <ENTY>56</ENTY>
            <success>true</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>103</QUEID>
            <ENTY>656</ENTY>
            <success>false</success>
        </Entity>
        <Entity>
            <Type/>
            <QUEID>104</QUEID>
            <ENTY>234</ENTY>
            <success>true</success>
        </Entity>
        </Entities>

进入

<Entities>
   <Entity>
      <Type/>
      <QUEID>100</QUEID>
      <ENTY>95</ENTY>
      <success>false</success>
   </Entity>
   <Entity>
      <Type/>
      <QUEID>101</QUEID>
      <ENTY>45</ENTY>
      <success>false</success>
   </Entity>
   <Entity>
      <Type/>
      <QUEID>103</QUEID>
      <ENTY>656</ENTY>
      <success>false</success>
   </Entity>
   <EntityList>100,101,103</EntityList>
</Entities>

这与您要求的略有不同,但我不确定为什么您想要的输出有

<Entity>
                <Type/>
                <QUEID>103</QUEID>
                <ENTY>56</ENTY>
                <success>false</success>
            </Entity>

因为该元素在输入中不存在。

于 2013-08-16T16:32:45.577 回答