简体   繁体   English

使用 xmlstarlet 添加 2 个或更多子节点

[英]add 2 or more subnodes with xmlstarlet

I would like to achive this result using xmlstarlet我想使用 xmlstarlet 来实现这个结果

<mrd:transferOptions>
            <mrd:MD_DigitalTransferOptions>
               <mrd:onLine>
                  <cit:CI_OnlineResource>
                     <cit:linkage>
                        <gco:CharacterString>text</gco:CharacterString>
                     </cit:linkage>
                     <cit:protocol>
                        <gco:CharacterString>WWW:LINK-1.0-http--link</gco:CharacterString>
                     </cit:protocol>
                     <cit:name>
                        <gco:CharacterString>text 1</gco:CharacterString>
                     </cit:name>
                     <cit:description>
                        <gco:CharacterString>Text 2</gco:CharacterString>
                     </cit:description>
                     <cit:function>
                        <cit:CI_OnLineFunctionCode codeList="http://standards.iso.org/iso/19115/resources/Codelists/cat/codelists.xml#CI_OnLineFunctionCode"
                                                   codeListValue="doi"/>
                     </cit:function>
                  </cit:CI_OnlineResource>
               </mrd:onLine>
            </mrd:MD_DigitalTransferOptions>
</mrd:transferOptions>

to be added to my XSL file I'm using the following code:要添加到我的 XSL 文件中,我使用以下代码:

xmlstarlet ed -N mdb="http://standards.iso.org/iso/19115/-3/mdb/2.0" -N mrd="http://standards.iso.org/iso/19115/-3/mrd/1.0" -N cit="http://standards.iso.org/iso/19115/-3/cit/2.0" -N gco="http://standards.iso.org/iso/19115/-3/gco/1.0" \
--subnode "//mdb:distributionInfo/mrd:MD_Distribution" -t elem -n 'mrd:transferOptions' -v "" \
--subnode '$prev' -t elem -n 'mrd:MD_DigitalTransferOptions' -v "" \
--subnode '$prev' -t elem -n 'mrd:onLine' -v "" \
--subnode '$prev' -t elem -n 'cit:CI_OnlineResource' -v "" \
--subnode '$prev' -t elem -n 'cit:linkage' -v "" \
--subnode '$prev' -t elem -n 'gco:CharacterString' -v "text" \
filx.xsl

to create the first part of my xml up to the element cit:linkage创建我的 xml 的第一部分,直到元素 cit:linkage

How can I add the remaining elements?如何添加剩余的元素?

I tried something like this to add the element cit:protocol我尝试了这样的方法来添加元素 cit:protocol

xmlstarlet ed -N mdb="http://standards.iso.org/iso/19115/-3/mdb/2.0" -N mrd="http://standards.iso.org/iso/19115/-3/mrd/1.0" -N cit="http://standards.iso.org/iso/19115/-3/cit/2.0" -N gco="http://standards.iso.org/iso/19115/-3/gco/1.0" \
--subnode "//mdb:distributionInfo/mrd:MD_Distribution" -t elem -n 'mrd:transferOptions' -v "" \
--subnode '$prev' -t elem -n 'mrd:MD_DigitalTransferOptions' -v "" \
--subnode '$prev' -t elem -n 'mrd:onLine' -v "" \
--subnode '$prev' -t elem -n 'cit:CI_OnlineResource' -v "" \
--subnode '$prev' -t elem -n 'cit:linkage' -v "" \
--subnode '$prev' -t elem -n 'gco:CharacterString' -v "https://doi.org/10.48784/15c8945c-534a-11ec-a1d1-02000a08f41d" \
--append  '($prev)[last()]' -t elem -n 'cit:protocol' -v '' \
--subnode '$prev' -t elem -n 'gco:CharacterString' -v "WWW:LINK-1.0-http--link" \
file.xsl

but the result is not correct.但结果不正确。 Could you tell me what I'm doing wrong?你能告诉我我做错了什么吗?

The $prev back reference is redefined by each -i , -a , and -s option so save the cit:CI_OnlineResource node in a variable: $prev反向引用由每个-i-a-s选项重新定义,因此将cit:CI_OnlineResource节点保存在变量中:

  -s '$prev' -t elem -n 'cit:CI_OnlineResource' \
  --var CI '$prev' \
  -s '$CI' -t elem -n 'cit:linkage' \
  -s '$prev' -t elem -n 'gco:CharacterString' -v 'text' \
  # etc.

-s (--subnode) xpath adds a node as last child of xpath . -s (--subnode) xpath添加一个节点作为xpath的最后一个子节点。 Alternatively, use the -a (--append) option to append a sibling node.或者,使用 append 兄弟节点的-a (--append)选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM