简体   繁体   English

使用C#进行xslt操作

[英]xslt manipulation using C#

I have a parent xslt that contains 100 fields elements(nested fields are present). 我有一个父xslt,其中包含100个字段元素(存在嵌套字段)。 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. 现在,我需要创建一个包含50个字段(基于excel文件)的子xslt,该字段保留与父对象相同的结构,并从父对象中删除一些不需要的字段。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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