Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:53:08+00:00 2026-06-17T21:53:08+00:00

XSLT 2 is preferred and I hope makes this easier. Given a document similar

  • 0

XSLT 2 is preferred and I hope makes this easier.

Given a document similar to

<doc xmlns:bob="bob.com">
    <element bob:name="fred" bob:occupation="Dr">Stuff</element>
    <element bob:name="Bill" bob:occupation="Dr" bob:birthMonth="Jan"/>
    <element>Kill Me</element>
    <element bob:name="fred" bob:occupation="Dr">different Stuff</element>
</doc>

I would like to have all of the unique elements based on all the attributes in bob namespace. This is a representative sample but I will have much deeper nesting so all I want it to traverse the tree for

*[@bob:*] and get the unique set of those.

The hoped for output would look like

<doc xmlns:bob="bob.com">
    <element bob:name="fred" bob:occupation="Dr">Stuff</element>
    <element bob:name="Bill" bob:occupation="Dr" bob:birthMonth="Jan"/>
</doc>

where one element was removed for not having any @bob:* attributes and the other was removed for being a duplicate of the first based solely on the attributes.

I was trying to use a key but didn’t seem to be doing it right

<xsl:key name="BobAttributes" match="//*" use="./@bob:*" />

I also tried creating a function that concatenated all the @bob attributes but that also didn’t seem to do what I hoped.

<xsl:key name="BobAttributes" match="//*" use="functx:AllBobConcat(.)" />

 <xsl:function name="functx:AllBobConcat" as="xs:string*" 
    xmlns:functx="http://www.functx.com" >
    <xsl:param name="nodes" as="node()*"/> 

    <xsl:for-each select="$nodes/@bob:*">
        <xsl:value-of select="local-name(.)"/>
        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:function>

In both cases I was using “simple” XSL to filter out the unique ones maybe I blew it here?
Variables added here to try and debug.

  <xsl:template match="*[@ism:*]" priority="100">
        <xsl:variable name="concat">
            <xsl:value-of select="functx:AllBobConcat(.)"/>
        </xsl:variable>

        <xsl:variable name="myID">
            <xsl:value-of select="generate-id() "/>
        </xsl:variable>

        <xsl:variable name="Keylookup">
            <xsl:value-of select="key('BobAttributes', $concat)"/>
        </xsl:variable>
        <xsl:value-of select="concat($concat, $Keylookup, $myID)"/>
        <xsl:if test="generate-id() = generate-id(key('BobAttributes', $concat)[1])">

            <xsl:apply-templates select="." mode="copy"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()" mode="copy">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

Looking forward to hearing what simple thing I overlooked or completely different cool approach I should have taken.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:53:10+00:00Added an answer on June 17, 2026 at 9:53 pm

    I would define the function AllBobConcat as you do, and then use this as a grouping key:

    <xsl:for-each-group select="element" group-by="f:AllBobConcat(.)">
      <xsl:if test="current-grouping-key() != ''">
        <xsl:copy-of select="current-group()[1]"/>
      </xsl:if>
    </xsl:for-each-group>
    

    Except that AllBobConcat needs to ensure the attributes are in a canonical order, so:

    <xsl:function name="f:AllBobConcat" as="xs:string">
        <xsl:param name="node" as="element(element)"/> 
        <xsl:value-of>
          <xsl:for-each select="$node/@bob:*">
            <xsl:sort select="local-name()"/>
            <xsl:value-of select="local-name(.)"/>
            <xsl:value-of select="'='"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="' '"/>
          </xsl:for-each>
        </xsl:value-of>
    </xsl:function>
    

    Also, you shouldn’t be putting your functions in a namespace that belongs to someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is XSLT a good solution for breaking an XML document into sets by element
I have a xslt like this: <?xml version=1.0 encoding=utf-8?> <xsl:stylesheet xmlns:db=http://www.liquibase.org/xml/ns/dbchangelog xmlns:xsl=http://www.w3.org/1999/XSL/Transform version=2.0> <xsl:output
Using XSLT 2.0: @ Linenumber 8370 this code: <TestCaseElement> <Name><![CDATA[DUT_AC_ON]]></Name> <TaggedValues> </TaggedValues> <Description> <Line><![CDATA[{TEXT_LANG}
Using XSLT, I'd like to be able to transform this : <doc> <tag1>AAA</tag1> Hello
My xslt transform does not pick up my document, this is because some/most elements
My xslt template looks like this: <xsl:template match=text()> <xsl:param name=precedingPStyle select=preceding-sibling::aic:pstyle[position()=1]/@name/> </xsl:template> Is above
XSLT / XML: convert apostrophe to a space My XML <Name>KUEN'S INTERNATIONAL</Name> USING XSL
I have an XSD element defined as the following: <xsd:simpleType name=string1500> <xsd:restriction base=xsd:string> <xsd:maxLength
I want return XSLT format in mvc what is the mime type for this?
In XSLT, how do you select/copy part of the document as text only? The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.