Remove/Avoid 项组合列表中的重复项
Remove/Avoid duplicates from combined list of items
我有一个 exsl:node-set
,其中包含我用作 查找 table 的项目数组,以显示 Location 来自我从 XML 文档本身构建的另一个 唯一列表 。查找工作正常,但是,我希望能够从结果数据集中删除重复项。
下图说明了这个问题。
XML 文档
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="default.xsl"?>
<TestResults>
<Result>
<TestID>1001</TestID>
<ResultValue>2.5</ResultValue>
<InstrumentID>503||1</InstrumentID>
</Result>
<Result>
<TestID>1002</TestID>
<ResultValue>6.3</ResultValue>
<InstrumentID>503||1</InstrumentID>
</Result>
<Result>
<TestID>1003</TestID>
<ResultValue>11</ResultValue>
<InstrumentID>342||1</InstrumentID>
</Result>
<Result>
<TestID>1004</TestID>
<ResultValue>201</ResultValue>
<InstrumentID>342||1</InstrumentID>
</Result>
<Result>
<TestID>1005</TestID>
<ResultValue>85</ResultValue>
<InstrumentID>731||1</InstrumentID>
</Result>
<Result>
<TestID>1006</TestID>
<ResultValue>33.6</ResultValue>
<InstrumentID>334||1</InstrumentID>
</Result>
</TestResults>
XLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:java="http://xml.apache.org/xslt/java"
xmlns:svg="http://www.w3.org/2000/svg">
<!-- INSTRUMENTS PER SITE ARRAY -->
<xsl:variable name="instruments-site">
<instrument ID='334||3'>Laboratory 1</instrument>
<instrument ID='342||1'>Laboratory 1</instrument>
<instrument ID='342||2'>Laboratory 1</instrument>
<instrument ID='503||1'>Laboratory 1</instrument>
<instrument ID='503||2'>Laboratory 1</instrument>
<instrument ID='503||3'>Laboratory 2</instrument>
<instrument ID='503||4'>Laboratory 2</instrument>
<instrument ID='731||1'>Laboratory 2</instrument>
</xsl:variable>
<xsl:variable name="list-instruments-site" select="exsl:node-set($instruments-site)" xmlns:exsl="http://exslt.org/common"/>
<!-- INSTRUMENT LIST-->
<xsl:key name="key-instruments" match="/TestResults/Result" use="InstrumentID"/>
<xsl:key name="key-instruments-list" match="/TestResults/Result/InstrumentID" use="."/>
<xsl:variable name="unique-instruments-list" select="//TestResults/Result/InstrumentID[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
<xsl:variable name="unique-location-list" select="$list-instruments-site/instrument[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
<xsl:template match="/TestResults">
<html>
<head>
<title>Test Document</title>
</head>
<body>
<h2>Instruments</h2>
<xsl:apply-templates select="$unique-instruments-list" mode="instr-locations" />
</body>
</html>
</xsl:template>
<xsl:template mode="instr-locations" match="text()">
<xsl:value-of select="." />
<br/>
</xsl:template>
</xsl:stylesheet>
我试过的
- 我尝试使用
$unique-location-list
构建第三个 唯一列表 ,如下所示:
<xsl:variable name="unique-location-list" select="$list-instruments-site/instrument[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
- 我也试过在
instr-locations
模板上使用 preceding-sibling
,像这样:
<xsl:variable name="previous" select="preceding-sibling::$list-instruments-site/instrument" />
<xsl:if test="$previous != current()">
<xsl:value-of select="$list-instruments-site/instrument[@ID=current()]" />
</xsl:if>
在此先感谢您的帮助。请记住,解决方案必须在 XSLT 1.0 中。
期望的HTML输出
我期待与此类似的 HTML 输出,回答@martinhonnen 的评论。
<ul>
<li>Laboratorio 1</li>
<li>Laboratorio 2</li>
</ul>
你能做这样的事情吗:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:variable name="labs">
<lab name="Laboratory 1">
<instrument ID="334||3"/>
<instrument ID="342||1"/>
<instrument ID="342||2"/>
<instrument ID="503||1"/>
<instrument ID="503||2"/>
</lab>
<lab name="Laboratory 2">
<instrument ID="503||3"/>
<instrument ID="503||4"/>
<instrument ID="731||1"/>
</lab>
</xsl:variable>
<xsl:template match="/TestResults">
<xsl:variable name="instrumentIDs" select="Result/InstrumentID" />
<html>
<body>
<xsl:for-each select="exsl:node-set($labs)/lab[instrument/@ID = $instrumentIDs]">
<xsl:value-of select="@name" /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我有一个 exsl:node-set
,其中包含我用作 查找 table 的项目数组,以显示 Location 来自我从 XML 文档本身构建的另一个 唯一列表 。查找工作正常,但是,我希望能够从结果数据集中删除重复项。
下图说明了这个问题。
XML 文档
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="default.xsl"?>
<TestResults>
<Result>
<TestID>1001</TestID>
<ResultValue>2.5</ResultValue>
<InstrumentID>503||1</InstrumentID>
</Result>
<Result>
<TestID>1002</TestID>
<ResultValue>6.3</ResultValue>
<InstrumentID>503||1</InstrumentID>
</Result>
<Result>
<TestID>1003</TestID>
<ResultValue>11</ResultValue>
<InstrumentID>342||1</InstrumentID>
</Result>
<Result>
<TestID>1004</TestID>
<ResultValue>201</ResultValue>
<InstrumentID>342||1</InstrumentID>
</Result>
<Result>
<TestID>1005</TestID>
<ResultValue>85</ResultValue>
<InstrumentID>731||1</InstrumentID>
</Result>
<Result>
<TestID>1006</TestID>
<ResultValue>33.6</ResultValue>
<InstrumentID>334||1</InstrumentID>
</Result>
</TestResults>
XLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:java="http://xml.apache.org/xslt/java"
xmlns:svg="http://www.w3.org/2000/svg">
<!-- INSTRUMENTS PER SITE ARRAY -->
<xsl:variable name="instruments-site">
<instrument ID='334||3'>Laboratory 1</instrument>
<instrument ID='342||1'>Laboratory 1</instrument>
<instrument ID='342||2'>Laboratory 1</instrument>
<instrument ID='503||1'>Laboratory 1</instrument>
<instrument ID='503||2'>Laboratory 1</instrument>
<instrument ID='503||3'>Laboratory 2</instrument>
<instrument ID='503||4'>Laboratory 2</instrument>
<instrument ID='731||1'>Laboratory 2</instrument>
</xsl:variable>
<xsl:variable name="list-instruments-site" select="exsl:node-set($instruments-site)" xmlns:exsl="http://exslt.org/common"/>
<!-- INSTRUMENT LIST-->
<xsl:key name="key-instruments" match="/TestResults/Result" use="InstrumentID"/>
<xsl:key name="key-instruments-list" match="/TestResults/Result/InstrumentID" use="."/>
<xsl:variable name="unique-instruments-list" select="//TestResults/Result/InstrumentID[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
<xsl:variable name="unique-location-list" select="$list-instruments-site/instrument[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
<xsl:template match="/TestResults">
<html>
<head>
<title>Test Document</title>
</head>
<body>
<h2>Instruments</h2>
<xsl:apply-templates select="$unique-instruments-list" mode="instr-locations" />
</body>
</html>
</xsl:template>
<xsl:template mode="instr-locations" match="text()">
<xsl:value-of select="." />
<br/>
</xsl:template>
</xsl:stylesheet>
我试过的
- 我尝试使用
$unique-location-list
构建第三个 唯一列表 ,如下所示:
<xsl:variable name="unique-location-list" select="$list-instruments-site/instrument[generate-id() = generate-id(key('key-instruments-list', .)[1])]"/>
- 我也试过在
instr-locations
模板上使用preceding-sibling
,像这样:
<xsl:variable name="previous" select="preceding-sibling::$list-instruments-site/instrument" />
<xsl:if test="$previous != current()">
<xsl:value-of select="$list-instruments-site/instrument[@ID=current()]" />
</xsl:if>
在此先感谢您的帮助。请记住,解决方案必须在 XSLT 1.0 中。
期望的HTML输出
我期待与此类似的 HTML 输出,回答@martinhonnen 的评论。
<ul>
<li>Laboratorio 1</li>
<li>Laboratorio 2</li>
</ul>
你能做这样的事情吗:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:variable name="labs">
<lab name="Laboratory 1">
<instrument ID="334||3"/>
<instrument ID="342||1"/>
<instrument ID="342||2"/>
<instrument ID="503||1"/>
<instrument ID="503||2"/>
</lab>
<lab name="Laboratory 2">
<instrument ID="503||3"/>
<instrument ID="503||4"/>
<instrument ID="731||1"/>
</lab>
</xsl:variable>
<xsl:template match="/TestResults">
<xsl:variable name="instrumentIDs" select="Result/InstrumentID" />
<html>
<body>
<xsl:for-each select="exsl:node-set($labs)/lab[instrument/@ID = $instrumentIDs]">
<xsl:value-of select="@name" /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>