3

我正在尝试使用属性获取 scala xml 节点标记。我想只获取带有属性的标签名称,而不是子元素。

我有这个输入:

<substance-classes>
    <nucleic-acid-sequence display-name="Nucleic Acid Sequence">
        <nucleic-acid-base>
            <base-symbol>a</base-symbol>
            <count>295</count>
        </nucleic-acid-base>
        <nucleic-acid-base>
            <base-symbol>c</base-symbol>
            <count>329</count>
        </nucleic-acid-base>
        <nucleic-acid-base>
            <base-symbol>g</base-symbol>
            <count>334</count>
        </nucleic-acid-base>
        <nucleic-acid-base>
            <base-symbol>t</base-symbol>
            <count>268</count>
        </nucleic-acid-base>
    </nucleic-acid-sequence>
    <genbank-information>
        <genbank-accession-number>EU186063</genbank-accession-number>
    </genbank-information>
</substance-classes>

我正在尝试<nucleic-acid-sequence>通过这样做来替换的内容

val newNucleicAcidSequenceNode = <nucleic-acid-sequence>{ myfunction 
} </nucleic-acid-sequence>

但有些<nucleic-acid-sequence>具有<nucleic-acid- sequence display-name="Nucleic Acid Sequence">. 由于 my newNucleicAcidSequenceNode是硬​​编码标签,因此我失去了属性。

如何保留可选属性并仍然传递{ myfunction }<nucleic-acid-sequence>标签?

4

1 回答 1

1

所以,如果我已经很好地理解了你:

  • 您只想替换 xml 的一部分
  • 这部分是任何nucleic-acid-sequence以下的孩子substance-classes
  • 你不想失去任何上述的任何属性nucleic-acid-sequence
  • 改变这些孩子是由一个函数 ( myFunction)

所以我的答案是在这种情况下:

import scala.xml.{Node, Elem}

val myXml: Elem =
      <substance-classes>
        <nucleic-acid-sequence display-name="Nucleic Acid Sequence">
          <nucleic-acid-base>
            <base-symbol>a</base-symbol>
            <count>295</count>
          </nucleic-acid-base>
          <nucleic-acid-base>
            <base-symbol>c</base-symbol>
            <count>329</count>
          </nucleic-acid-base>
          <nucleic-acid-base>
            <base-symbol>g</base-symbol>
            <count>334</count>
          </nucleic-acid-base>
          <nucleic-acid-base>
            <base-symbol>t</base-symbol>
            <count>268</count>
          </nucleic-acid-base>
        </nucleic-acid-sequence>
        <genbank-information>
          <genbank-accession-number>EU186063</genbank-accession-number>
        </genbank-information>
      </substance-classes>

def myFunction(children: Seq[Node]) : Seq[Node] = ??? // whatever you want it to be

// Here's the replacement:

myXml.copy(child = myXml.child.map {
  case e@Elem(_, "nucleic-acid-sequence", _, _, children@_*) =>
    e.asInstanceOf[Elem].copy(child = myFunction(children))
  case other => other
})

例如,myFunction可以只保留计数超过 300 的孩子,可能是这样的:

import scala.util.{ Try, Success }
def myFunction(children: Seq[Node]): Seq[Node] = children.collect {
  case e: Node if Try((e \ "count").text.toInt > 300) == Success(true) =>
  e
}

在这种情况下,如果你用这个替换myFunction第一个片段中未实现的,替换将给出:

  <substance-classes>
    <nucleic-acid-sequence display-name="Nucleic Acid Sequence"><nucleic-acid-base>
        <base-symbol>c</base-symbol>
        <count>329</count>
      </nucleic-acid-base><nucleic-acid-base>
        <base-symbol>g</base-symbol>
        <count>334</count>
      </nucleic-acid-base></nucleic-acid-sequence>
    <genbank-information>
      <genbank-accession-number>EU186063</genbank-accession-number>
    </genbank-information>
  </substance-classes>

如您所见,没有任何属性nucleic-acid-sequence丢失,并且您的函数为定义的条件保留了超过四个的两个节点。

希望能帮助到你。

于 2019-04-10T17:49:20.297 回答