XSLT 删除重复项
我有一个 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) > 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 XSLT 1.0,您可以使用“Muenchian 方法”。
您将创建一个键,通过
@region
和@dept
值的组合来索引Row
元素。然后,您将获得该区域/部门组合的第一次出现(具有所需区域 (
$DeptQS
))并输出部门 (dept
)。下面是一个输出
元素以显示上下文的示例样式表:这是使用输入 XML 的输出:
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:Here's the output using your input XML:
该样式表:
输出:
This stylesheet:
Output:
以下样式表显示了在多个级别进行分组的一般方法:
它会根据您的输入生成以下输出:
The following stylesheet shows a general approach to grouping at multiple levels:
It produces the following output on your input:
您还没有显示出您想要的输出。但在 XSLT 2.0 中,您可以执行以下操作:
You haven't shown your desired output. But in XSLT 2.0, you could do something like this: