需要通过 XSLT 获取不同的元素

发布于 2024-12-06 23:19:56 字数 1480 浏览 2 评论 0原文

我在名为 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>

包含重复项的元素列表

我引用了 如何在 XSLT 中选择唯一节点但我不是能够做对。

I have a list of table elements under element named Query that may duplicate, I need to fetch distinct table elements (not its value but the tag/element name itself.

/ShopArea/Connection/Query/* lists the table names including duplicates.

Below is the 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>

List of elements including duplicates

I refered How to select unique nodes in XSLT but I'm not able to get it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-12-13 23:19:56

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

<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>
</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>

生成所需的正确结果(查询子元素的所有不同名称的元素)

TABLE1
TABLE2
TABLE3
TABLE21
TABLE22
TABLE23

II。 XSLT 2.0:此转换使用

<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(), '
'"/>
     </xsl:for-each-group>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

I. XSLT 1.0. This transformation uses simple 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>
</xsl:text>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

When applied on the provided XML document:

<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>

the wanted, correct result (all different names of elements that are children of a Query) is produced:

TABLE1
TABLE2
TABLE3
TABLE21
TABLE22
TABLE23

II. XSLT 2.0: This transformation uses <xsl:for-each-group>:

<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(), '
'"/>
     </xsl:for-each-group>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文