2014-10-09 2 views
1

XQuery를 처음 사용했습니다. 이 입력을 바꾸기 위해 XQuery를 사용할 방법이 있습니까?값을 변경하고 새 요소를 만드는 방법은 무엇입니까?

<mods> 
    <subject> 
     <topic> bird ; cat ; dog ; lion </topic> 
    </subject> 
</mods> 

을 다음 XML로 변환 할 수 있습니까?

<mods> 
    <subject> 
     <topic> bird </topic><topic> cat </topic><topic> dog </topic><topic> lion </topic> 
    </subject> 
</mods> 

제가

함수
replace(node,';','</topic><topic>') 

사용하려고하지만 <> 브래킷 그 개체 참조 &lt; 또는 &gt;에 각 변.

답변

2

귀하는 XQuery 프로세서는 XQuery 업데이트를 지원하는 경우, 다음과 같은 쿼리를 시도 할 수 있습니다 :

copy $xml := document { 
    <mods> 
    <subject> 
     <topic> bird ; cat ; dog ; lion </topic> 
    </subject> 
    </mods> 
} 
modify (
    let $topic := $xml/mods/subject/topic 
    return replace node $topic with 
    for $token in tokenize($topic, ';') 
    return <topic>{ $token }</topic> 
) 
return $xml 
2

replace(...)은 요소가 아닌 문자열에서만 작동합니다. '</topic><topic>' (문자열) 및 </topic><topic> (잘못된 XML)은 동일하지 않습니다!

그들 각각에 대한 새로운 주제 항목을 작성, (당신은 또한 XQuery를 업데이트를 사용할 수 있습니다) 결과를 재구성하고, 각 <topic/> 요소를 토큰 화 :

element mods { 
    element subject { 
    for $topics in /mods/subject/topic 
    for $topic in tokenize($topics, ';') 
    return element topic { $topic } 
    } 
} 

를 새로운 요소를 구성, 나는 계산을 선호 위에 사용 된 요소 생성자. element nodeName { $content }<nodeName>{ $content }</nodeName>을 사용하는 것과 같습니다.

관련 문제