Delete Nodes + attributes that match Xpath except specific attributes
- by Ryan Ternier
I'm trying to find the best (efficient) way of doing this.
I have a medium sized XML document. Depending on specific settings certain portions of it need to be filtered out for security reasons.
I'll be doing this in XSLT as it's configurable and no code should need changing.
I've looked around, but not getting much luck on it.
For example:
I have the following XPath:
//*[@root='2.16.840.1.113883.3.51.1.1.6.1']
Whicrooth gives me all nodes with a root attribute equal to a specific OID. In these nodes I want to have all attributes except for a few (ex. foo and bar) erased, and then having another attribute added (ex. reason)
I also need to have multiple XPath expressions that can be ran to zero down on a specific node and clear it's contents out in a similar fashion, with respect to nodes with specific attributes.
I'm playing around with information from:
XPath expression to select all XML child nodes except a specific list?
and XSLT Remove Elements and/or Attributes by Name per XSL Parameters
Will update shortly when I can have access what what I"ve done so far.
Example:
XML Before Transformation
<root>
<childNode>
<innerChild root="2.16.840.1.113883.3.51.1.1.6.1" a="b" b="c" type="innerChildness"/>
<innerChildSibling/>
</childNode>
<animals>
<cat>
<name>bob</name>
</cat>
</animals>
<tree/>
<water root="2.16.840.1.113883.3.51.1.1.6.1" z="zed" l="ell" type="liquidLIke"/>
</root>
After
<root>
<childNode>
<innerChild root="2.16.840.1.113883.3.51.1.1.6.1" flavor="MSK"/> <!-- filtered -->
<innerChildSibling/>
</childNode>
<animals>
<cat flavor="MSK" /> <!-- cat was filtered -->
</animals>
<tree/>
<water root="2.16.840.1.113883.3.51.1.1.6.1" flavor="MSK"/> <!-- filtered -->
</root>