Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xpath question

From: "Anthony Jones" <Ant@------------.--->
To: 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




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent