我怎样才能得到href属性值出的<?xml样式表>节点? [英] How can I get the href attribute value out of an <?xml-stylesheet> node?

查看:123
本文介绍了我怎样才能得到href属性值出的<?xml样式表>节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都可以从我们需要执行使用他们的样式表XSL转换,使我们可以得到的HTML转换为PDF供应商的XML文档。实际的样式表是在引用的href 的属性?XML文档中的xml样式表定义。有什么办法,我可以得到的网址了使用C#?我不相信供应商不改变网址,显然不想硬编码。



XML文件的充分<$ C $开始?C> xml样式表元素看起来是这样的:

 < XML版本=1.0 编码=UTF-8>?; 
<?xml样式表类型=文/ XSL的href =htt​​p://www.fakeurl.com/StyleSheet.xsl>


解决方案

的LINQ to XML代码:

  XDOC的XDocument = ...; 

VAR cssUrlQuery =从xDoc.Nodes节点()
,其中node.NodeType == XmlNodeType.ProcessingInstruction
选择Regex.Match(((XProcessingInstruction)节点)。数据, HREF = \(小于?URL方式> *?)\)组。[网址]值。

或LINQ的对象。



  VAR cssUrls =(XmlNode的距离在childNode doc.ChildNodes 
,其中childNode.NodeType == XmlNodeType.ProcessingInstruction&放大器;&安培; childNode.Name ==xml样式表
选择(XmlProcessingInstruction)childNode
到procNode选择Regex.Match(procNode.Data,HREF = \(小于?URL方式> *?)\)。组[网址]值) .ToList();因为



xDoc.XPathSelectElement()将不会工作一段reasone不能施放的XElement到XProcessingInstruction。


We are getting an XML document from a vendor that we need to perform an XSL transform on using their stylesheet so that we can convert the resulting HTML to a PDF. The actual stylesheet is referenced in an href attribute of the ?xml-stylesheet definition in the XML document. Is there any way that I can get that URL out using C#? I don't trust the vendor not to change the URL and obviously don't want to hardcode it.

The start of the XML file with the full ?xml-stylesheet element looks like this:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.fakeurl.com/StyleSheet.xsl"?>

解决方案

Linq to xml code:

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;

or linq to objects

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() will not work since it for some reasone cannot cast an XElement to XProcessingInstruction.

这篇关于我怎样才能得到href属性值出的&LT;?xml样式表&GT;节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆