Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - xpath question [Thread Next] Re: xpath questionTo: NULL Date: 2/7/2008 5:19:00 PM
"ms" <bg@b...> wrote in message
news:O%23O7icaaIHA.5348@T......
> hi all,
>
> I have a doc that looks like this:
>
> <A>
> <B></B>
> <B></B>
> <B></B>
> <B></B>
> <B></B>
> <C></C>
> <C></C>
> <C></C>
> <Z></Z>
> </A>
>
> I would like to be able to pull out the unique (DISTINCT as I know it)
> values under A. i.e. the results I would like are:
>
> <B></B>
> <C></C>
> <Z></Z>
>
> I can't work out get the xpath to get these results, it must be something
> like "A/*[not(name() = preceding-sibling::A/*name())]" but my little brain
> can't work it out.
>
> Anyone?
It always helps if you specify the context, XSL? MSXML, .NET, other?
Assuming xsl then:-
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" />
<xsl:template match="/A">
<xsl:for-each select="*">
<xsl:if test="not(preceding-sibling::*[local-name() =
local-name(current())])">
<xsl:value-of select="local-name()" /><br />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
simple but for large sets not very effecient. This is an alternative that
builds an index using xsl:key
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" />
<xsl:key name="tagNames" match="/A/*" use="local-name()" />
<xsl:template match="/A">
<xsl:for-each select="*[count(.| key('tagNames', local-name())[1]) = 1]">
<xsl:value-of select="local-name()" /><br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
If not XSL then the language you are using will probably have some
dictionary like construct that can help.
--
Anthony Jones - MVP ASP/ASP.NET
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
