我使用以下xml和xsl文件获取重复记录。我只想转换一组列表项。如果可能,请尽量不要从xsl部分中删除任何内容(只需添加到其中)。
<?xml version="1.0" encoding="utf-8" ?>
<data>
<listitems name="Select..." CtrId="Id2"/>
<listitems name="Item A" CtrId="Id2"/>
<listitems name="Item B" CtrId="Id2"/>
<listitems name="Select..." CtrId="Id4"/>
<listitems name="Item A" CtrId="Id4"/>
<listitems name="Item B" CtrId="Id4"/>
<listitems name="Select..." CtrId="Id6"/>
<listitems name="Item C" CtrId="Id6"/>
<listitems name="Item D" CtrId="Id6"/>
</data> <xsl:template match="data/listitems">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<xsl:value-of select="@name"/>
</body>
</html>
</xsl:template>结果(错误行为;重复)选择...物料A物料B选择...项目A项目B
所需行为(仅获取1组)选择...项目A项目B
发布于 2012-08-02 21:38:46
下面是一种相对简单的方法:
<xsl:param name="useId" select="/data/listitems[1]/@CtrId" />
<xsl:template match="/">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<xsl:apply-templates select="data/listitems[@CtrId = $useId]"/>
</body>
</html>
</xsl:template>
<xsl:template match="listitems">
<xsl:value-of select="concat(@name, ' ')" />
</xsl:template>您现有的模板实际上会为每个listitems元素放入一个html元素-看起来您很可能只需要一个。
顶部的<xsl:param>声明挑选文件中的第一个CtrId,并使用它。您可以使用select="'Id2'"将其更改为文字值(请注意双引号中的单引号),或者可以将一个参数传递到带有您想要选取的ID的样式表中。
发布于 2012-08-02 12:21:29
此转换
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<xsl:apply-templates select=
"*[starts-with(@name,'Select')][1]"/>
</body>
</html>
</xsl:template>
<xsl:template match="*[starts-with(@name,'Select')]">
<xsl:apply-templates mode="inGroup" select=
"(.|following-sibling::*
[generate-id(following-sibling::*
[@name[starts-with(.,'Select')]][1]
)
=
generate-id(current()/following-sibling::*
[@name[starts-with(.,'Select')]][1])
]
)/@name
"/>
</xsl:template>
<xsl:template match="@name" mode="inGroup">
<xsl:value-of select="concat(., ' ')"/>
</xsl:template>
</xsl:stylesheet>在所提供的XML文档上应用时的
<data>
<listitems name="Select..." CtrId="Id2"/>
<listitems name="Item A" CtrId="Id2"/>
<listitems name="Item B" CtrId="Id2"/>
<listitems name="Select..." CtrId="Id4"/>
<listitems name="Item A" CtrId="Id4"/>
<listitems name="Item B" CtrId="Id4"/>
</data>会生成想要的正确结果:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
</head>
<body>Select... Item A Item B </body>
</html>https://stackoverflow.com/questions/11770707
复制相似问题