views:

77

answers:

1

Hello,

I need to write a function that takes a sequence of "tag" elements of the form:

<tag type="markupType" value="topic"/> 
<tag type="concept" value="death"/>
...

and turns them into attributes of the form

data-markupType="topic"
data-concept="death"

So far I have the following function:

declare function local:tagsToAttrs($tags as element()*) as attribute()*
{
    for $tag in $tags
    let $type := $tag/string(@type)
    let $value := $tag/string(@value)
    return
        attribute { concat('data-', $type) } { $value }
};

This is working fine so far, but I need to deal with the case where I have two or more tags with the same "type". In this case I cannot have two attributes with the same name, so I want to have a single attribute with space separated values...

e.g.

<tag type="concept" value="death"/>
<tag type="concept" value="life"/>
<tag type="concept" value="birth"/>

would become

data-concept="death life birth"

I've been stuck on this for a while now - so if anyone has a nice way of modifying my function to do this I'd be much obliged.

Pls note I don't want to use XSLT for this. I want to use XQuery.

Kind Regards

Swami

A: 

For each distinct type, get the attribute value by joining the values of tags with that type:

declare function local:tagsToAttrs($tags as element()*) as attribute()*
{
    for $type in distinct-values($tags/@type)
    let $value := string-join($tags[@type=$type]/@value," ")
    return
        attribute { concat('data-', $type) } { $value }
};
Chris Wallace