YAML中管道符号有什么用? [英] What is the use of the pipe symbol in YAML?

查看:60
本文介绍了YAML中管道符号有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yaml 的新手,我有一个关于用于多行的管道符号 (|) 的问题.YAML 是否有类似下面的语法?

I am new to yaml, and I have a question about the pipe symbol (|) used for multiple lines. Does YAML have any syntax like the one below?

测试:|6+

test: |6+

在下面的两个 YAML 文件中,第一个有效,第二个无效.我不知道是什么原因造成的.

Of the two YAML files below, the first one is working and second is not. I do not know what is causing this.

第一个文件

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |
     a
     aa
     aaa
     aaaa : 'test:'

第二个文件

Name :
  -
   testing:
     val1
  -
   second:
     val2
  -
   third:
     val3
  -
   then
  - 
    final: |6+
      a
      aa
      aaa
      aaaa : 'test:'

第二个文件是客户的.

我正在使用 XMLBeans,但出现此错误:

I am using XMLBeans and I get this error:

com.esotericsoftware.yamlbeans.parser.Parser$ParserException:第 17 行,第 12 列:预期为块结束"但发现:块映射开始.

推荐答案

YAML 中行尾的管道符号表示后面的任何缩进文本都应解释为多行标量值.请参阅YAML 规范.

The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec.

具体来说,管道指示(除了缩进)标量值应该以保留换行符的方式按字面解释.相反,> 字符表示后面跟着多行折叠"标量,这意味着换行符被转换为空格.例如:

Specifically, the pipe indicates that (except for the indentation) the scalar value should be interpreted literally in such a way that preserves newlines. Conversely, the > character indicates that multi-line "folded" scalar follows, meaning that newlines are converted to spaces. For example:

>>> import yaml
>>> yaml.load("""
... |
...  This is a multi-line
...  literal style scalar.
... """)
'This is a multi-line\nliteral style scalar.\n'
>>> yaml.load("""
... >
...  This is a multi-line
...  folded scalar; new lines are folded into
...  spaces.
... """)
'This is a multi-line folded scalar; new lines are folded into spaces.\n'

6+ 部分是缩进指示符(明确说明应该使用多少个缩进空格),带有chomping 指示符"+,它控制额外应处理标量文字末尾的空格.

The 6+ part is the indentation indicator (an explicit specification of how many spaces of indentation should be used) with the "chomping indicator" + which controls how extra whitespace at the end of the scalar literal should be handled.

你得到的错误是一个棘手的错误:这是因为缩进应该相对于当前的块级元素.所以在这种情况下它应该是 4+ 而不是 6+ 因为最后一个块级元素是数组项(由 - 指定)并且文字从它缩进4.有点令人惊讶的是 final: | 映射不被视为块元素,即使它的值是多行的.如果您考虑一下,这有点有意义——它仍然只是一个单行"键:值"映射.该值恰好对多行标量值使用了特殊语法.令人困惑,但不知何故一致......

The error you're getting is a tricky one: It's because indentation should be relative to the current block-level element. So in this case it should be 4+ instead of 6+ because the last block-level element is the array item (specified by -) and the literal is indented 4 from it. Somewhat surprisingly the final: | mapping is not considered a block element even though its value is multi-lined. It sort of makes sense if you think about it -- it's still just a 'one-liner' "key: value" mapping. The value just happens to be using a special syntax for multi-line scalar values. Confusing, but somehow consistent...

这篇关于YAML中管道符号有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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