I have a list of terms in xml. Terms either have termtype nd or pt. If a term is termtype nd, it has no ID, but instead references a termtype PT which does have an ID. I need to do two things.
-
replace the XXX for termId value with an incremental number for termtype nd, so all terms have an id.
-
where a termType ND references a termType PT, I need to somehow look for its ID and insert it:
<Zthes>
<term>
<termName>Term 1</termName>
<termId>insert new term id N+1 </termId>
<termType>Nd</termType>
<relation>
<relationType>USE</relationType>
<termId>insert ID of term 2 <termID>
<termName>Term2</termName>
</relation>
</term>
<term>
<termName>Term 2</termName>
<termId>587889</termId>
<termType>pt</termType>
</term>
</zthes>
Could anyone give me pointers on how to do this. I don’t know anything about xslt. I’ve looked on here and found stuff about find and replace with Unique but from what I understand of the answer, I don’t think it is applicable.
ALso, It doesn’t have to be xslt. I work with a developer who said that this was the best way to do this, but that the answer was too trivial for him to bother with. Its too difficult for me though!
This single transformation:
when applied on the following XML document (your XML but corrected to be syntactically well-formed and semantically correct, then enlarged twice to be made more interesting):
produces the wanted, correct result:
Do note:
The identity rule is used to copy every node “as-is”.
Templates override the identity rule only for elements that need to be modified.
<xsl:number>is used for (me being lazy) calculating the required termId.A key is used for most efficient finding a
termIdby itsTermName