【问题标题】:Where does the OwnedName type come from?OwnedName 类型从何而来?
【发布时间】:2015-03-21 07:14:33
【问题描述】:

我正在尝试使用 rust-xml 解析 rust 中的 XML 文件,但在匹配标签名称时遇到问题:

for e in parser.events() {
    match e {
        XmlEvent::StartElement { name, attributes: _, namespace: _ } => {
            match name {
                "LexicalEntry" => {

这是我收到的错误消息:

enter codesrc/main.rs|127 col 21| 127:35 error: mismatched types:
||  expected `xml::name::OwnedName`,
||     found `&'static str`
|| (expected struct `xml::name::OwnedName`,
||     found &-ptr) [E0308]
|| src/main.rs:127                     "LexicalEntry" => {
||                                     ^~~~~~~~~~~~~~
 here

我觉得这很令人惊讶,因为 OwnedName 标识符没有出现在我项目的代码或依赖项中的任何地方(包括 rust 源!):

$ rgrep OwnedName 。二进制文件 ./woodenboek/src/.main.rs.swp 匹配 ./woodenboek/src/main.rs:
//xml::name::OwnedName("LexicalEntry") => { 二进制文件 ./woodenboek/target/debug/deps/libxml-5882f08ff8adc5e5.rlib 匹配

我应该匹配的这个 OwnedName 类型来自哪里?编译器是否出于某种原因发明了某种类型并将其插入到 xml 库中?

【问题讨论】:

    标签: xml xml-parsing rust


    【解决方案1】:

    非常抱歉,我还没有将 xml-rs 的 API 文档放在任何地方 :( 我会尽快解决这个问题。

    更新修复了这个问题,你可以找到最新的文档here。例如这里是OwnedName。

    OwnedName 是一个单独的结构,因为 XML 名称不仅仅是字符串——它们由本地名称、命名空间 URI 和可选前缀组成,因此它们具有特殊的表示形式。为了只检查本地名称,您可以使用OwnedName 的local_name 字段,这是String:

    for e in parser.events() {
        match e {
            XmlEvent::StartElement { name, attributes: _, namespace: _ } => {
                match &name.local_name[..] {
                    "LexicalEntry" =>
    

    【讨论】:

      【解决方案2】:

      xml::name::OwnedName 是XmlEvent::StartElement 变体的name 字段的类型,而不是&'static str(字符串文字)。

      【讨论】:

      • 在我的机器上,该变体只是一个“名称”。所以我认为这是 Cargo 的一些奇怪之处。似乎 Cargo 为 rust-xml 签出的代码与构建 rlib 的版本不同,后者发现它已进入我的二进制文件。
      猜你喜欢
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2011-04-18
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多