注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

xml

从 XML 文件读取结构化数据。

XML 文件将被解析为由字典和字符串构成的数组。 XML 节点可以是元素或字符串。其中,元素将转化为字典字典和如下键值

示例中的 XML 文件包含一个 news 根标签,其中包含多个 article 标签。每个 article 标签都有一个 titleauthorcontent 标签。content 标签包含一个或多个段落,表示为 p 标签。

举例

#let find-child(elem, tag) = {
  elem.children
    .find(e => "tag" in e and e.tag == tag)
}

#let article(elem) = {
  let title = find-child(elem, "title")
  let author = find-child(elem, "author")
  let pars = find-child(elem, "content")

  heading(title.children.first())
  text(10pt, weight: "medium")[
    Published by
    #author.children.first()
  ]

  for p in pars.children {
    if (type(p) == "dictionary") {
      parbreak()
      p.children.first()
    }
  }
}

#let data = xml("example.xml")
#for elem in data.first().children {
  if (type(elem) == "dictionary") {
    article(elem)
  }
}
Preview

参数
参数是函数的输入,它们在函数名称后面的括号中传入。

xml() -> any

path
str
必需参数位置参数
位置参数按顺序传入,不带名称。

XML 文件路径。

定义
函数和类型可以有与其关联的定义 (成员或方法)。可以使用 "." 操作符来访问调用它们。

decode

从 XML 字符串/字节流读取结构化数据。

xml.decode() -> any

data
str bytes
必需参数位置参数
位置参数按顺序传入,不带名称。

XML 数据。