代码之家  ›  专栏  ›  技术社区  ›  AJ.

如何从<?xml样式表>节点中获取href属性值?

  •  9
  • AJ.  · 技术社区  · 16 年前

    href 属性 ?xml-stylesheet

    元素如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="http://www.fakeurl.com/StyleSheet.xsl"?>
    
    6 回复  |  直到 16 年前
        1
  •  3
  •   Mikael Svenson    16 年前

    XDocument xDoc = ...;
    
    var cssUrlQuery = from node in xDoc.Nodes()
            where node.NodeType == XmlNodeType.ProcessingInstruction
            select Regex.Match(((XProcessingInstruction)node).Data, "href=\"(?<url>.*?)\"").Groups["url"].Value;
    

    或linq到对象

    var cssUrls = (from XmlNode childNode in doc.ChildNodes
                       where childNode.NodeType == XmlNodeType.ProcessingInstruction && childNode.Name == "xml-stylesheet"
                       select (XmlProcessingInstruction) childNode
                       into procNode select Regex.Match(procNode.Data, "href=\"(?<url>.*?)\"").Groups["url"].Value).ToList();
    

    xDoc.XPathSelectElement()将无法工作,因为出于某些原因,它无法将XElement强制转换为XProcessingInstruction。

        2
  •  4
  •   Martin Honnen    16 年前

    由于处理指令可以包含任何内容,因此它在形式上没有任何属性。但是如果您知道存在“伪”属性,比如xml样式表处理指令,那么您当然可以使用处理指令的值来构造单个元素的标记,并使用xml解析器解析该标记:

        XmlDocument doc = new XmlDocument();
        doc.Load(@"file.xml");
        XmlNode pi = doc.SelectSingleNode("processing-instruction('xml-stylesheet')");
        if (pi != null)
        {
            XmlElement piEl = (XmlElement)doc.ReadNode(XmlReader.Create(new StringReader("<pi " + pi.Value + "/>")));
            string href = piEl.GetAttribute("href");
            Console.WriteLine(href);
        }
        else
        {
            Console.WriteLine("No pi found.");
        }
    
        3
  •  3
  •   Ishmael    16 年前

    您还可以使用XPath。给定加载了源代码的XmlDocument:

    XmlProcessingInstruction instruction = doc.SelectSingleNode("//processing-instruction(\"xml-stylesheet\")") as XmlProcessingInstruction;
    if (instruction != null) {
        Console.WriteLine(instruction.InnerText);
    }
    

        4
  •  2
  •   Pent Ploompuu    16 年前

    要使用适当的XML解析器查找值,可以编写如下内容:

    
    using(var xr = XmlReader.Create(input))
    {
        while(xr.Read())
        {
            if(xr.NodeType == XmlNodeType.ProcessingInstruction && xr.Name == "xml-stylesheet")
            {
                string s = xr.Value;
                int i = s.IndexOf("href=\"") + 6;
                s = s.Substring(i, s.IndexOf('\"', i) - i);
                Console.WriteLine(s);
                break;
            }
        }
    }
    
        5
  •  1
  •   user1167761    12 年前
    private string _GetTemplateUrl(XDocument formXmlData) 
    {
        var infopathInstruction = (XProcessingInstruction)formXmlData.Nodes().First(node => node.NodeType == XmlNodeType.ProcessingInstruction && ((XProcessingInstruction)node).Target == "mso-infoPathSolution");
        var instructionValueAsDoc = XDocument.Parse("<n " + infopathInstruction.Data + " />");
        return instructionValueAsDoc.Root.Attribute("href").Value;
    }
    
        6
  •  0
  •   Asinthe    9 年前

    XmlProcessingInstruction stylesheet=doc。选择SingleNode(“处理指令('xml-stylesheet'))作为XmlProcessingInstruction;