开发者

xquery function to convert tag elements to attributes

开发者 https://www.devze.com 2022-12-31 16:42 出处:网络
I need to write a function that takes a sequence of \"tag\" elements of the form: <tag type=\"markupType\" value=\"topic\"/>

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开发者_JS百科 $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


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 }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消