简体   繁体   中英

xslt manipulation using C#

I have a parent xslt that contains 100 fields elements(nested fields are present). Now i need to create a child xslt that contains 50 fields(based on an excel file) retaining the same structure as the parent, removing a few unwanted fields fron the parent. Any input on how do i proceed.

Assuming you have already figured out how to populate an array (or other collection) with the list of fields to remove, you should be able to do something like this:

string[] fields = new string[] { "c", "d" };
// In actuality, the assumption is that your XmlDocument is loaded some other way
XmlDocument document = new XmlDocument();
document.LoadXml("<ABCD> <a>1</a> <b>2</b> <c>3</c> <d>4</d> </ABCD>");

string xpath = "//*[" + fields.Select(v => "self::" + v)
                              .Aggregate((a, b) => a + " or " + b) 
                      + "]";
List<XPathNavigator> nodes = document.CreateNavigator().Select(xpath).Cast<XPathNavigator>().ToList();
foreach (XPathNavigator node in nodes)
{
    node.DeleteSelf();
}

string resultDoc = document.OuterXml;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM