开发者

CodeMirror 2: How to filter out xml attributes?

开发者 https://www.devze.com 2023-03-29 12:56 出处:网络
Is there a way to mod开发者_如何学Goify CodeMirror or XML mode definition script to enable filtering of few attributes that user shouldn\'t see on screen?

Is there a way to mod开发者_如何学Goify CodeMirror or XML mode definition script to enable filtering of few attributes that user shouldn't see on screen?

So I just want to find that attributes and give them new class that is set to 'display:none'

For example...

before:

<sample1 xns:id="e7b014d9-6271-4e32-921d-7488edfd6ea4">a</sample1>
<sample2 xns:id="d3450e86-7264-4512-9891-6c7183257741">b</sample2>
<sample3 xns:id="7f04f178-f235-4647-8584-c4e77f73fecf">c</sample3>

after:

<sample1>a</sample1>
<sample2>b</sample2>
<sample3>c</sample3>

And I don't want to delete attributes from XML itself I just want to hide them, because I need them when I convert editing result back to XML object.


You may try to use xslt to convert schema into format you want. Not replace file but generate anather with hiden attributes.


Here is my C# code to do the job. You can easily modify it into JavaScript.

public static string RemoveAttributes(
        string xmlString)  {
  string retXML = null;
  XmlDocument xDoc = new XmlDocument();
  xDoc.LoadXml(xmlString);
  XmlNode root = xDoc.DocumentElement;
  if (xDoc.DocumentElement != null) {
     XmlNodeList list = xDoc.SelectNodes(@"/");

     if ( list != null ) {
        RemoteAttributes(list);
        retXML = root.OuterXml;
     }
  }

  return retXML;
}

private static void RemoteAttributes(XmlNodeList list) {
  if (list != null ) {
    foreach (XmlNode node in list) {
      if (node.Attributes != null) {
        node.Attributes.RemoveAll();
      }
      if (node.HasChildNodes) {
        RemoteAttributes(node.ChildNodes);
      }
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消