【问题标题】:Need to fetch distinct elements through XSLT需要通过 XSLT 获取不同的元素
【发布时间】:2011-09-28 21:02:01
【问题描述】:

我在名为 Query 的元素下有一个可能重复的表元素列表,我需要获取不同的表元素(不是它的值,而是标签/元素名称本身。

/ShopArea/Connection/Query/* 列出包含重复项的表名。

下面是 XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="ShopArea.xslt"?>
<ShopArea>
<Connection name="Connection1">
    <Report date="25-09-2011">

        <Query id="1">
            <TABLE1>1.1</TABLE1>
            <TABLE2>1.2</TABLE2>
            <TABLE3>1.3</TABLE3>
        </Query>
        <Query id="2">
            <TABLE21>2.1</TABLE21>
            <TABLE22>2.2</TABLE22>
            <TABLE23>2.3</TABLE23>
        </Query>
    </Report>

    <Report date="26-09-2011">
        <Query id="1">
            <TABLE1>26 1.1</TABLE1>
            <TABLE2>26 1.2</TABLE2>
            <TABLE3>26 1.3</TABLE3>
        </Query>
        <Query id="2">
            <TABLE21>26 2.1</TABLE21>
            <TABLE22>26 2.2</TABLE22>
            <TABLE23>26 2.3</TABLE23>
        </Query>
    </Report>
</Connection>
</ShopArea>

我推荐了How to select unique nodes in XSLT,但我无法正确理解。

【问题讨论】:

    标签: xml xslt duplicates


    【解决方案1】:

    我。 XSLT 1.0。此转换使用简单的Muenchian grouping

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:key name="kChildByName" match="Query/*"
      use="name()"/>
    
     <xsl:template match=
      "Query/*
           [generate-id()
           =
            generate-id(key('kChildByName', name())[1])
           ]">
         <xsl:value-of select="name()"/>
         <xsl:text>&#xA;</xsl:text>
     </xsl:template>
    
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时

    <ShopArea>
        <Connection name="Connection1">
            <Report date="25-09-2011">
                <Query id="1">
                    <TABLE1>1.1</TABLE1>
                    <TABLE2>1.2</TABLE2>
                    <TABLE3>1.3</TABLE3>
                </Query>
                <Query id="2">
                    <TABLE21>2.1</TABLE21>
                    <TABLE22>2.2</TABLE22>
                    <TABLE23>2.3</TABLE23>
                </Query>
            </Report>
            <Report date="26-09-2011">
                <Query id="1">
                    <TABLE1>26 1.1</TABLE1>
                    <TABLE2>26 1.2</TABLE2>
                    <TABLE3>26 1.3</TABLE3>
                </Query>
                <Query id="2">
                    <TABLE21>26 2.1</TABLE21>
                    <TABLE22>26 2.2</TABLE22>
                    <TABLE23>26 2.3</TABLE23>
                </Query>
            </Report>
        </Connection>
    </ShopArea>
    

    产生了想要的正确结果(Query 的子元素的所有不同名称)

    TABLE1
    TABLE2
    TABLE3
    TABLE21
    TABLE22
    TABLE23
    

    二。 XSLT 2.0:此转换使用 &lt;xsl:for-each-group&gt;:

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/*/*">
         <xsl:for-each-group select="Report/Query/*"
                             group-by="name()">
          <xsl:sequence select="current-grouping-key(), '&#xA;'"/>
         </xsl:for-each-group>
     </xsl:template>
    
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    【讨论】:

    • 太棒了!它可以工作,但是这段代码(Muenchian 分组)看起来太复杂了。需要一段时间才能理解。非常感谢。
    • @Arvind Singh:不客气。 Muenchian 分组是高效 XSLT 1.0 分组的事实标准。为了更好地理解它,只需点击此答案第一行的链接 - 这将导致 Jeni Tennison 出色的 Muenchian Grouping 页面。
    猜你喜欢
    • 2013-11-04
    • 2016-11-02
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2020-08-14
    • 2015-09-02
    相关资源
    最近更新 更多