XSLT 删除重复项

发布于 2024-10-29 03:03:30 字数 1515 浏览 1 评论 0原文

我有一个 xml 数据源,如下所示:

<dsQueryResponse>
  <Department>
    <Rows>
      <Row dept="HR" state="NY" region="East"/>
      <Row dept="HR" state="NJ" region="East"/>
      <Row dept="SD" state="NY" region="East"/>
      <Row dept="MM" state="NY" region="East"/>
      <Row dept="SD" state="NJ" region="East"/>
      <Row dept="SD" state="CO" region="West"/>
      <Row dept="MM" state="CO" region="West"/>
    </Rows>
  </Department>
</dsQueryResponse>

我的 XSLT 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="no"/>
  <xsl:decimal-format NaN=""/>
  <xsl:param name="DeptQS">East</xsl:param>
  <xsl:variable name="deptRows" select="/dsQueryResponse/Department/Rows/Row[@region = $DeptQS]"/>

  <xsl:template match="/">
    <xsl:if test="count($deptRows) &gt; 0">
      <xsl:call-template name="deptList"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="deptList">
    <xsl:for-each select="$deptRows">
      <!-- process unique depts--> 
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

我想要写出该地区的所有部门。我现在拥有的代码将写出重复的部门。但是我怎样才能只写一次该地区的部门呢?

谢谢!

I have an xml datasource which looks like this:

<dsQueryResponse>
  <Department>
    <Rows>
      <Row dept="HR" state="NY" region="East"/>
      <Row dept="HR" state="NJ" region="East"/>
      <Row dept="SD" state="NY" region="East"/>
      <Row dept="MM" state="NY" region="East"/>
      <Row dept="SD" state="NJ" region="East"/>
      <Row dept="SD" state="CO" region="West"/>
      <Row dept="MM" state="CO" region="West"/>
    </Rows>
  </Department>
</dsQueryResponse>

My XSLT looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="no"/>
  <xsl:decimal-format NaN=""/>
  <xsl:param name="DeptQS">East</xsl:param>
  <xsl:variable name="deptRows" select="/dsQueryResponse/Department/Rows/Row[@region = $DeptQS]"/>

  <xsl:template match="/">
    <xsl:if test="count($deptRows) > 0">
      <xsl:call-template name="deptList"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="deptList">
    <xsl:for-each select="$deptRows">
      <!-- process unique depts--> 
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

I want to get all the departments in the region written out. The code I have now will write out duplicate departments. But how can I write out the department for the region only once?

Thanks!

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

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

发布评论

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

评论(4

如梦初醒的夏天 2024-11-05 03:03:31

对于 XSLT 1.0,您可以使用“Muenchian 方法”。

您将创建一个键,通过 @region@dept 值的组合来索引 Row 元素。

然后,您将获得该区域/部门组合的第一次出现(具有所需区域 ($DeptQS))并输出部门 (dept)。

下面是一个输出 元素以显示上下文的示例样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="kDept" match="Department/Rows/Row" use="concat(@region,'|',@dept)"/>
  <xsl:param name="DeptQS">East</xsl:param>

  <xsl:template match="/*">
    <xsl:for-each select="Department/Rows/Row[count(.|key('kDept',
      concat($DeptQS,'|',@dept))[1])=1]">
      <test>Current dept: <xsl:value-of select="@dept"/></test>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

这是使用输入 XML 的输出:

<test>Current dept: HR</test>
<test>Current dept: SD</test>
<test>Current dept: MM</test>

For XSLT 1.0 you can use the "Muenchian Method".

You would create a key to index the Row elements by a combination of @region and @dept values.

Then you would get the first occurrence of that region/department combination (that has the wanted region ($DeptQS)) and output the department (dept).

Here's an example stylesheet outputting <test> elements to show the context:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="kDept" match="Department/Rows/Row" use="concat(@region,'|',@dept)"/>
  <xsl:param name="DeptQS">East</xsl:param>

  <xsl:template match="/*">
    <xsl:for-each select="Department/Rows/Row[count(.|key('kDept',
      concat($DeptQS,'|',@dept))[1])=1]">
      <test>Current dept: <xsl:value-of select="@dept"/></test>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Here's the output using your input XML:

<test>Current dept: HR</test>
<test>Current dept: SD</test>
<test>Current dept: MM</test>
巾帼英雄 2024-11-05 03:03:31

我怎样才能写出部门
该区域仅一次?

该样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pRegion" select="'East'"/>
    <xsl:key name="kRowByDept-Region" match="Row"
             use="concat(@dept,'++',@region)"/>
    <xsl:template match="Rows">
        <xsl:apply-templates select="Row[generate-id()
                                          = generate-id(
                                               key('kRowByDept-Region',
                                                   concat(@dept,'++',$pRegion)
                                               )[1]
                                            )
                                     ]"/>
    </xsl:template>
    <xsl:template match="Row">
        <xsl:value-of select="concat(@dept,'
')"/>
    </xsl:template>
</xsl:stylesheet>

输出:

HR
SD
MM

how can I write out the department for
the region only once?

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pRegion" select="'East'"/>
    <xsl:key name="kRowByDept-Region" match="Row"
             use="concat(@dept,'++',@region)"/>
    <xsl:template match="Rows">
        <xsl:apply-templates select="Row[generate-id()
                                          = generate-id(
                                               key('kRowByDept-Region',
                                                   concat(@dept,'++',$pRegion)
                                               )[1]
                                            )
                                     ]"/>
    </xsl:template>
    <xsl:template match="Row">
        <xsl:value-of select="concat(@dept,'
')"/>
    </xsl:template>
</xsl:stylesheet>

Output:

HR
SD
MM
最佳男配角 2024-11-05 03:03:31

以下样式表显示了在多个级别进行分组的一般方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="byRegion" match="Row" use="@region" />
    <xsl:key name="byRegionState" 
             match="Row" use="concat(@region, '|', @state)" />
    <xsl:key name="byRegionStateDept" 
             match="Row" use="concat(@region, '|', @state, '|', @dept)" />
    <xsl:template 
         match="Row[generate-id() = generate-id(key('byRegion', @region)[1])]">
        <region name="{@region}">
            <xsl:apply-templates
                select="key('byRegion', @region)
                         [generate-id() =
                          generate-id(key('byRegionState',
                                      concat(@region, '|', @state))[1])]"
                mode="states" />
        </region>
    </xsl:template>
    <xsl:template match="Row" mode="states">
        <state name="{@state}">
            <xsl:apply-templates
                select="key('byRegionState', concat(@region, '|', @state))
                         [generate-id() =
                          generate-id(key('byRegionStateDept',
                                concat(@region, '|', @state, '|', @dept))[1])]"
                mode="dept" />
        </state>
    </xsl:template>
    <xsl:template match="Row" mode="dept">
        <dept><xsl:value-of select="@dept" /></dept>
    </xsl:template>
</xsl:stylesheet>

它会根据您的输入生成以下输出:

<region name="East">
    <state name="NY">
        <dept>HR</dept>
        <dept>SD</dept>
        <dept>MM</dept>
    </state>
    <state name="NJ">
        <dept>HR</dept>
        <dept>SD</dept>
    </state>
</region>
<region name="West">
    <state name="CO">
        <dept>SD</dept>
        <dept>MM</dept>
    </state>
</region>

The following stylesheet shows a general approach to grouping at multiple levels:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="byRegion" match="Row" use="@region" />
    <xsl:key name="byRegionState" 
             match="Row" use="concat(@region, '|', @state)" />
    <xsl:key name="byRegionStateDept" 
             match="Row" use="concat(@region, '|', @state, '|', @dept)" />
    <xsl:template 
         match="Row[generate-id() = generate-id(key('byRegion', @region)[1])]">
        <region name="{@region}">
            <xsl:apply-templates
                select="key('byRegion', @region)
                         [generate-id() =
                          generate-id(key('byRegionState',
                                      concat(@region, '|', @state))[1])]"
                mode="states" />
        </region>
    </xsl:template>
    <xsl:template match="Row" mode="states">
        <state name="{@state}">
            <xsl:apply-templates
                select="key('byRegionState', concat(@region, '|', @state))
                         [generate-id() =
                          generate-id(key('byRegionStateDept',
                                concat(@region, '|', @state, '|', @dept))[1])]"
                mode="dept" />
        </state>
    </xsl:template>
    <xsl:template match="Row" mode="dept">
        <dept><xsl:value-of select="@dept" /></dept>
    </xsl:template>
</xsl:stylesheet>

It produces the following output on your input:

<region name="East">
    <state name="NY">
        <dept>HR</dept>
        <dept>SD</dept>
        <dept>MM</dept>
    </state>
    <state name="NJ">
        <dept>HR</dept>
        <dept>SD</dept>
    </state>
</region>
<region name="West">
    <state name="CO">
        <dept>SD</dept>
        <dept>MM</dept>
    </state>
</region>
简单气质女生网名 2024-11-05 03:03:30

您还没有显示出您想要的输出。但在 XSLT 2.0 中,您可以执行以下操作:

<xsl:template match="Rows">
  <xsl:for-each-group select="Row" group-by="@region">
    <region name="{current-grouping-key()}">
      <xsl:for-each-group select="current-group()" group-by="@dept">
        <dept name="{current-grouping-key()}">
          <xsl:for-each select="current-group()">
            <state name="{@state}"/>
          </xsl:for-each>
        </dept>
      </xsl:for-each-group>
    </region>
  </xsl:for-each>
</xsl:template>

You haven't shown your desired output. But in XSLT 2.0, you could do something like this:

<xsl:template match="Rows">
  <xsl:for-each-group select="Row" group-by="@region">
    <region name="{current-grouping-key()}">
      <xsl:for-each-group select="current-group()" group-by="@dept">
        <dept name="{current-grouping-key()}">
          <xsl:for-each select="current-group()">
            <state name="{@state}"/>
          </xsl:for-each>
        </dept>
      </xsl:for-each-group>
    </region>
  </xsl:for-each>
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文