这是我在
here年发布的问题的副本
我只需要注意这是在teiid引擎的XMLTABLE中发生的.我正在解析以下xml内容:
我只需要注意这是在teiid引擎的XMLTABLE中发生的.我正在解析以下xml内容:
<AttributeSets> <ns2:ItemAttributes xml:lang="de-DE"> <ns2:Creator Role="Role1">Creator One</ns2:Creator> <ns2:Creator Role="Role2">Creator Two</ns2:Creator> <ns2:Creator Role="Role3">Creator Three</ns2:Creator> </ns2:ItemAttributes> </AttributeSets>
运用
Select * From XMLTABLE(XMLNAMESPACES( DEFAULT 'http://ns1','http://ns2/default.xsd' as ns2 ),'AttributeSets/ns2:ItemAttributes' PASSING x.AttributeSets COLUMNS Creator STRING PATH 'string-join(//ns2:Creator/(@Role,node()),'','')' ) p;
这里x.AttributeSets是一个xml类型的变量,其内容来自上面.
我正在尝试使用xpath格式化并将其组合成一行.
就像是:
string-join(//ns2:Creator/concat(./text(),@Role),',')
我想,我在某个地方很近,因为这个:
string-join(//ns2:Creator/@Role,')
工作并给我一个以逗号分隔的角色列表:Role1,Role2,Role3
还有这个
string-join(//ns2:Creator/node(),')
结合创作者的价值观:“造物主一,造物主二,造物主三”.
我想要最后的输出
Role1: Creator One,Role2: Creator Two,Role3: Creator Three
我能得到的最多是:
string-join(//ns2:Creator/(@Role,')
这个逗号将所有角色和创建者分成一行.由于某种原因,concat运算符似乎无法工作.
能否请你帮忙.
解决方法
尝试以下xpath
string-join(for $n in /AttributeSets/ItemAttributes/Creator return concat($n/@Role,':',$n/text()),')
请记住根据源树上下文调整for中的选择器xpath.因此,我认为文件根
/AttributeSets/ItemAttributes/Creator
更新:
I think,i’m somewhere close,because this: … works and gives me a comma-separated list of roles: Role1,Role3
认为你很亲密.我尝试了一个小的调整,这工作:
string-join(//ns2:Creator/concat(@Role,./text()),')
For some reason the concat operator seems no to work.
不确定,我检查了一个在线xpath测试仪here,它完美地工作.事实上,在线工具也接受你的xapth以及以下输出:
Creator OneRole1,Creator TwoRole2,Creator ThreeRole3