XSLT: Modify attribute value based on sibling attribute value -
i have following xml:
<?xml version="1.0" encoding="utf-8"?> <odm fileoid="some oid" description="some desc" creationdatetime="2010-10-06t22:35:00-06:00" filetype="snapshot" > <study oid="s_test"> <globalvariables> <studyname>study_name</studyname> <studydescription> study description </studydescription> <protocolname>some protocol name</protocolname> </globalvariables> <metadataversion oid="v1.0.0" name="metadataversion_v1.0.0"> <protocol> <studyeventref studyeventoid="se01" ordernumber="36" mandatory="yes"/> <studyeventref studyeventoid="se02" ordernumber="37" mandatory="yes"/> </protocol> <studyeventdef oid="se01" name="some name 01" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"/> <formref formoid="f02" mandatory="yes"/> </studyeventdef> <studyeventdef oid="se02" name="some name 02" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"/> <formref formoid="f02" mandatory="yes"/> </studyeventdef> <formdef oid="f01" name="some form name 01" repeating="no"> <itemgroupref itemgroupoid="ig01" mandatory="yes"/> </formdef> <formdef oid="f02" name="some form name 02" repeating="no"> <itemgroupref itemgroupoid="ig02" mandatory="yes"/> <itemgroupref itemgroupoid="ig03" mandatory="no"/> </formdef> <itemgroupdef oid="ig01" name="ungrouped" repeating="no" sasdatasetname="ungroupe"> <itemref itemoid="i01" ordernumber="1" mandatory="yes"/> <itemref itemoid="i02" ordernumber="2" mandatory="yes"/> <itemref itemoid="i03" ordernumber="3" mandatory="yes"/> </itemgroupdef> <itemgroupdef oid="ig02" name="reasons2" repeating="yes" sasdatasetname="reasons2"> <itemref itemoid="i04" ordernumber="1" mandatory="no"/> </itemgroupdef> <itemgroupdef oid="ig03" name="ungrouped" repeating="no" sasdatasetname="ungro002"> <itemref itemoid="i05" ordernumber="1" mandatory="yes"/> <itemref itemoid="i06" ordernumber="2" mandatory="yes"/> </itemgroupdef> </metadataversion> </study> </odm>
what want is, each <itemgroupdef>
element "name" attribute equal "ungrouped", change attribute value "xxx-ungrouped", xxx value of oid attribute <formdef>
element has child <itemgroupref
> itemgroupoid attribute equal oid atrribute of itemgroupdef element. of other attributes , of children of element should left unaltered.
for example, <itemgroupdef oid="ig01">
above, name attribute should changed "ungrouped" "f01-ungrouped" since oid, ig01, appears child of <formdef oid="f01">
element. guaranteed itemgroupdef oid value appears child of 1 <formdef>
element.
the transformation should act on <itemgroupdef>
elements name attribute value of "ungrouped" only. of <itemgroupdef>
elements name attribute other "ungrouped" , of other elements in xml file should left unaltered.
this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:key name="koidbyitemgroup" match="formdef/@oid" use="../itemgroupref/@itemgroupoid"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="itemgroupdef/@name[.='ungrouped']"> <xsl:attribute name="name"> <xsl:value-of select="concat(key('koidbyitemgroup',../@oid), '-',.)"/> </xsl:attribute> </xsl:template> </xsl:stylesheet>
output:
<odm fileoid="some oid" description="some desc" creationdatetime="2010-10-06t22:35:00-06:00" filetype="snapshot"> <study oid="s_test"> <globalvariables> <studyname>study_name</studyname> <studydescription> study description </studydescription> <protocolname>some protocol name</protocolname> </globalvariables> <metadataversion oid="v1.0.0" name="metadataversion_v1.0.0"> <protocol> <studyeventref studyeventoid="se01" ordernumber="36" mandatory="yes"></studyeventref> <studyeventref studyeventoid="se02" ordernumber="37" mandatory="yes"></studyeventref> </protocol> <studyeventdef oid="se01" name="some name 01" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"></formref> <formref formoid="f02" mandatory="yes"></formref> </studyeventdef> <studyeventdef oid="se02" name="some name 02" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"></formref> <formref formoid="f02" mandatory="yes"></formref> </studyeventdef> <formdef oid="f01" name="some form name 01" repeating="no"> <itemgroupref itemgroupoid="ig01" mandatory="yes"></itemgroupref> </formdef> <formdef oid="f02" name="some form name 02" repeating="no"> <itemgroupref itemgroupoid="ig02" mandatory="yes"></itemgroupref> <itemgroupref itemgroupoid="ig03" mandatory="no"></itemgroupref> </formdef> <itemgroupdef oid="ig01" name="f01-ungrouped" repeating="no" sasdatasetname="ungroupe"> <itemref itemoid="i01" ordernumber="1" mandatory="yes"></itemref> <itemref itemoid="i02" ordernumber="2" mandatory="yes"></itemref> <itemref itemoid="i03" ordernumber="3" mandatory="yes"></itemref> </itemgroupdef> <itemgroupdef oid="ig02" name="reasons2" repeating="yes" sasdatasetname="reasons2"> <itemref itemoid="i04" ordernumber="1" mandatory="no"></itemref> </itemgroupdef> <itemgroupdef oid="ig03" name="f02-ungrouped" repeating="no" sasdatasetname="ungro002"> <itemref itemoid="i05" ordernumber="1" mandatory="yes"></itemref> <itemref itemoid="i06" ordernumber="2" mandatory="yes"></itemref> </itemgroupdef> </metadataversion> </study> </odm>
note: overwriting indentity rule. keys cross reference. node set keys.
from comments:
one question: xslt outputs elements
<formref formoid="f01" mandatory="yes"></formref>
when original elements<formref formoid="f01" mandatory="yes"/>
. both of these correct xml standards, there way tell xslt output elements in same exact format?
maybe (tested msxsl, altova, saxon, oracle, xqsharp)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:key name="koidbyitemgroup" match="formdef/@oid" use="../itemgroupref/@itemgroupoid"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="*[not(node())]"> <xsl:element name="{name()}"> <xsl:apply-templates select="@*"/> </xsl:element> </xsl:template> <xsl:template match="itemgroupdef/@name[.='ungrouped']"> <xsl:attribute name="name"> <xsl:value-of select="concat(key('koidbyitemgroup',../@oid), '-',.)"/> </xsl:attribute> </xsl:template> </xsl:stylesheet>
output:
<odm fileoid="some oid" description="some desc" creationdatetime="2010-10-06t22:35:00-06:00" filetype="snapshot"> <study oid="s_test"> <globalvariables> <studyname>study_name</studyname> <studydescription> study description </studydescription> <protocolname>some protocol name</protocolname> </globalvariables> <metadataversion oid="v1.0.0" name="metadataversion_v1.0.0"> <protocol> <studyeventref studyeventoid="se01" ordernumber="36" mandatory="yes"/> <studyeventref studyeventoid="se02" ordernumber="37" mandatory="yes"/> </protocol> <studyeventdef oid="se01" name="some name 01" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"/> <formref formoid="f02" mandatory="yes"/> </studyeventdef> <studyeventdef oid="se02" name="some name 02" repeating="no" type="scheduled"> <formref formoid="f01" mandatory="yes"/> <formref formoid="f02" mandatory="yes"/> </studyeventdef> <formdef oid="f01" name="some form name 01" repeating="no"> <itemgroupref itemgroupoid="ig01" mandatory="yes"/> </formdef> <formdef oid="f02" name="some form name 02" repeating="no"> <itemgroupref itemgroupoid="ig02" mandatory="yes"/> <itemgroupref itemgroupoid="ig03" mandatory="no"/> </formdef> <itemgroupdef oid="ig01" name="f01-ungrouped" repeating="no" sasdatasetname="ungroupe"> <itemref itemoid="i01" ordernumber="1" mandatory="yes"/> <itemref itemoid="i02" ordernumber="2" mandatory="yes"/> <itemref itemoid="i03" ordernumber="3" mandatory="yes"/> </itemgroupdef> <itemgroupdef oid="ig02" name="reasons2" repeating="yes" sasdatasetname="reasons2"> <itemref itemoid="i04" ordernumber="1" mandatory="no"/> </itemgroupdef> <itemgroupdef oid="ig03" name="f02-ungrouped" repeating="no" sasdatasetname="ungro002"> <itemref itemoid="i05" ordernumber="1" mandatory="yes"/> <itemref itemoid="i06" ordernumber="2" mandatory="yes"/> </itemgroupdef> </metadataversion> </study> </odm>
Comments
Post a Comment