【问题标题】:Using XSLT 1.0 to insert element in middle of XML List使用 XSLT 1.0 在 XML 列表中间插入元素
【发布时间】:2017-08-14 17:45:56
【问题描述】:

我有一个 XML 1.0 文档,需要使用 XSLT 1.0 文件进行转换。我需要转换的 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>

我需要在元素&lt;item guidRef="0f948c18-f326-40e5-9beb-2efc73803797"/&gt;之后插入&lt;item guidRef="21c1f231-e03e-48e8-916a-d8790442b417"/&gt;

所以列表将如下所示:

<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>

如何使用 XSLT 1.0 做到这一点?

我已经做了几次尝试,现在我的代码大部分都在工作。剩下的一个问题是如何在之后插入元素。下面的代码除了...

<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']">

它会生成这个 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" />

如何让它附加在元素之后而不是作为子元素插入?

【问题讨论】:

  • 你到底在哪里坚持这个?发布您的尝试,以便我们修复它,而不必从头开始为您编写代码。
  • @michael.hor257k 通常,我会这样做。到目前为止,我的问题是我对 xslt 知之甚少,所以到目前为止我的尝试还远远不够,甚至还没有接近。无论如何我都会添加它们。
  • 如果您需要一个起点,请以identity transform 开头,然后添加另一个与item[@guidRef='0f948c18-f326-40e5-9beb-2efc73803797'] 匹配的模板。该模板应复制当前的item,并创建新的item
  • @DanielHaley 我已经有了&lt;xsl:template match="uiConfig/commandBars"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select = "node()|@*" /&gt; 代码。我特别迷失了如何匹配项目并在其后插入。
  • @BenHoffman XSLT 1.0 中没有 except 运算符。我建议你听从 Daniel Haley 的建议。

标签: xml xslt


【解决方案1】:

这个解决方案奏效了:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多