使用 xpath 为每个元素查找不同的子元素
Find distinct subchild element for each element with xpath
我只是迷失了使用 xsl1.0
当数据被分成多个子子项时,我试图获得每个 'row' 的不同输出。
提供此数据源..
<?xml version="1.0" encoding="utf-8"?>
<Root>
<row>
<data>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>BM</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>FORD</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>BRAVO</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
</data>
</row>
<row>
<data>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>CHARLIE</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>BM</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
</data>
</row>
</Root>
我想要得到如下所示的输出。
BM, FORD, VAUXHALL, ALPHA, BRAVO,
CHARLIE, ALPHA, VAUXHALL, BM,
如果之前有人问过这个问题,我很抱歉,但我不知道如何 select 每行的不同值。我当前的结果仅显示第一行中的值,其他行中的任何重复值均未显示。
如有任何帮助,我们将不胜感激。
目前我的 xsl 是
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="Root">
<xsl:apply-templates select="row"/>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates select="data/a/b/c/Vehicle[not(ManufacturerName=preceding::Vehicle/ManufacturerName)]"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="data/a/b/c/Vehicle">
<xsl:value-of select="ManufacturerName"/>
<xsl:text>,</xsl:text>
</xsl:template>
</xsl:stylesheet>
有了这个结果。
BM,FORD,VAUXHALL,ALPHA,BRAVO,
CHARLIE,
首先,我建议您使用 Muenchian grouping 来获取不同的值,而不是您现在使用的低效方法。
要对每个 row
执行单独的分组,请在分组键中包含 row'
的唯一 ID:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="vehicle" match="Vehicle" use="concat(ManufacturerName, '|', generate-id(ancestor::row))" />
<xsl:template match="/Root">
<xsl:for-each select="row">
<xsl:variable name="row-id" select="generate-id()" />
<!-- unique vahicles in this row -->
<xsl:for-each select="data/a/b/c/Vehicle[count(. | key('vehicle', concat(ManufacturerName, '|', $row-id))[1]) = 1]">
<xsl:value-of select="ManufacturerName"/>
<xsl:text>, </xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
注意一些XSLT 1.0处理器支持EXSLT set:distinct()扩展功能,这使得这种事情变得简单多了。
我只是迷失了使用 xsl1.0 当数据被分成多个子子项时,我试图获得每个 'row' 的不同输出。
提供此数据源..
<?xml version="1.0" encoding="utf-8"?>
<Root>
<row>
<data>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>BM</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>FORD</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>BRAVO</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
</data>
</row>
<row>
<data>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>CHARLIE</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>VAUXHALL</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
<a>
<b>
<c>
<Vehicle>
<ManufacturerName>BM</ManufacturerName>
</Vehicle>
</c>
<c>
<Vehicle>
<ManufacturerName>ALPHA</ManufacturerName>
</Vehicle>
</c>
</b>
</a>
</data>
</row>
</Root>
我想要得到如下所示的输出。
BM, FORD, VAUXHALL, ALPHA, BRAVO,
CHARLIE, ALPHA, VAUXHALL, BM,
如果之前有人问过这个问题,我很抱歉,但我不知道如何 select 每行的不同值。我当前的结果仅显示第一行中的值,其他行中的任何重复值均未显示。
如有任何帮助,我们将不胜感激。
目前我的 xsl 是
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="Root">
<xsl:apply-templates select="row"/>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates select="data/a/b/c/Vehicle[not(ManufacturerName=preceding::Vehicle/ManufacturerName)]"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="data/a/b/c/Vehicle">
<xsl:value-of select="ManufacturerName"/>
<xsl:text>,</xsl:text>
</xsl:template>
</xsl:stylesheet>
有了这个结果。
BM,FORD,VAUXHALL,ALPHA,BRAVO,
CHARLIE,
首先,我建议您使用 Muenchian grouping 来获取不同的值,而不是您现在使用的低效方法。
要对每个 row
执行单独的分组,请在分组键中包含 row'
的唯一 ID:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="vehicle" match="Vehicle" use="concat(ManufacturerName, '|', generate-id(ancestor::row))" />
<xsl:template match="/Root">
<xsl:for-each select="row">
<xsl:variable name="row-id" select="generate-id()" />
<!-- unique vahicles in this row -->
<xsl:for-each select="data/a/b/c/Vehicle[count(. | key('vehicle', concat(ManufacturerName, '|', $row-id))[1]) = 1]">
<xsl:value-of select="ManufacturerName"/>
<xsl:text>, </xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
注意一些XSLT 1.0处理器支持EXSLT set:distinct()扩展功能,这使得这种事情变得简单多了。