【问题标题】:Get xml-stylesheet when using xml type provider?使用 xml 类型提供程序时获取 xml-stylesheet?
【发布时间】:2016-07-08 22:10:58
【问题描述】:

如何使用 xml 类型提供程序获取 xml-stylesheet

let xml = XmlProvider<"""<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type='text/xsl' href='/stylesheets/application_internet.xsl'?>
<application>......</application>""").GetSample()

let stylesheetHref = xml.....?

期待字符串'/stylesheets/application_internet.xsl'

【问题讨论】:

    标签: f# f#-data


    【解决方案1】:

    据我所知,使用 TypeProviders(或 Linq to XML)获取处理指令和相关数据并不容易。

    不过也可以这样:

    对于您的示例 XML GetSample 仅返回根元素内容,即 ......。稍微改变一下就可以让我们访问根目录XElement。知道处理节点是它的前一个兄弟节点,我们可以得到一个XProcessingInstruction 并从它的Data 中提取url

    #I "../packages/FSharp.Data.2.2.5/lib/net40"    
    #r "System.Xml.Linq"
    #r "FSharp.Data.dll"
    
    open FSharp.Data
    open System.Text.RegularExpressions
    open System.Xml.Linq
    
    let href s = Regex.Match(s, "href='(?<url>.*?)'").Groups.["url"].Value
    
    type Xml = XmlProvider<"""<?xml version="1.0" encoding="iso-8859-1"?>
    <?xml-stylesheet type='text/xsl' href='/stylesheets/application_internet.xsl'?>
    <application><other></other></application>""">
    
    let doc = Xml.GetSample()
    
    let stylesheetProcessing = (doc.XElement.PreviousNode :?> XProcessingInstruction)
    
    // /stylesheets/application_internet.xsl
    let url = href stylesheetProcessing.Data
    

    很明显,这段代码期望 XML 总是在同一个地方有一个有效的指令。添加错误处理作为练习:-)

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多