简体   繁体   English

使用XSLT 1.0在XML列表的中间插入元素

[英]Using XSLT 1.0 to insert element in middle of XML List

I have an XML 1.0 document that I need to transform with a XSLT 1.0 file. 我有一个XML 1.0文档,需要使用XSLT 1.0文件进行转换。 The XML I need to transform looks like this: 我需要转换的XML如下所示:

<commandBarData guid="f3016f3c-2847-4557-b61a-a2d05319cf18">
  <menubar>
    <modeData guid="76d73481-9076-44c9-821c-52de9408cce2">
      <item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/>
      <item guidRef="6c91d5ab-d648-4364-96fb-3e71bcfaf70d"/>
      <item guidRef="71f8ffd6-46bd-43a3-8256-5412bc2d7741"/>
      <item guidRef="ac291790-gf51-d4s1-f23x-dsf9dfb6fgf5"/>
    </modeData>
  </menubar>
</commandBarData>

I need to insert <item guidRef="21c1f231-e03e-48e8-916a-d8790442b417"/> after the element <item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/> 我需要在元素<item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/>之后插入<item guidRef="21c1f231-e03e-48e8-916a-d8790442b417"/> <item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/>

So the list will look like this: 因此,列表将如下所示:

<commandBarData guid="f3016f3c-2847-4557-b61a-a2d05319cf18">
  <menubar>
    <modeData guid="76d73481-9076-44c9-821c-52de9408cce2">
      <item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/>
      <item guidRef="21c1f231-e03e-48e8-916a-d8790442b417"/>
      <item guidRef="6c91d5ab-d648-4364-96fb-3e71bcfaf70d"/>
      <item guidRef="71f8ffd6-46bd-43a3-8256-5412bc2d7741"/>
      <item guidRef="ac291790-gf51-d4s1-f23x-dsf9dfb6fgf5"/>
    </modeData>
  </menubar>
</commandBarData>

How do I do this using XSLT 1.0? 如何使用XSLT 1.0做到这一点?

I have made several attempts and now I have code that is mostly working. 我做了几次尝试,现在我的代码可以正常工作了。 The one issue remaining is how to insert the element after. 剩下的一个问题是如何在其后插入元素。 The code below works except... 以下代码适用,但...

<xsl:template match="uiConfig/commandBars"> 
  <xsl:copy> 
    <xsl:apply-templates select = "node()|@*" />
  </xsl:copy>
</xsl:template>

<xsl:template 

match="uiConfig/commandBars/commandBarData/menubar/modeData/item[@guidRef='0f948c18-f326-40e5-9beb-2efc73803797']"> 匹配= “uiConfig /命令栏/ commandBarData /菜单栏/ modeData /项目[@ guidRef = '0f948c18-f326-40e5-9beb-2efc73803797']”>

It generates this xml: 它生成此xml:

<item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"><item guidRef="21c1f231-e03e-48e8-916a-d8790442b417" xmlns:frmwrk="Corel Framework Data" /></item>
      <item guidRef="21c1f231-e03e-48e8-916a-d8790442b417" />
      <item guidRef="6c91d5ab-d648-4364-96fb-3e71bcfaf70d" />
      <item guidRef="71f8ffd6-46bd-43a3-8256-5412bc2d7741" />
      <item guidRef="ac291790-gf51-d4s1-f23x-dsf9dfb6fgf5" />

How do I make it append after the element and not insert as a child? 如何使其附加在元素之后而不作为子元素插入?

This solution worked: 此解决方案有效:

<xsl:template match="uiConfig/commandBars/commandBarData/menubar/modeData/item[@guidRef='0f948c18-f326-40e5-9beb-2efc73803797']">
  <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
  <item guidRef="21c1f231-e03e-48e8-916a-d8790442b417"/>
</xsl:template>

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

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