【问题标题】:XSLT 2.0 build element dynamicallyXSLT 2.0 动态构建元素
【发布时间】:2013-05-07 21:10:47
【问题描述】:

我想编写一个 xslt 来转换以下 XML:

<instruments>
<instrument>1111-A01</instrument>
<instrument>1111-A02</instrument>
<instrument>2222-A03</instrument>
<instrument>2222-A04</instrument>
</instruments>

到以下 XML:

<references>
<reference>
    <id_celex>1111</id_celex>
    <article>A01</article>
    <article>A02</article>
</reference>
<reference>
    <id_celex>2222</id_celex>
    <article>A03</article>
    <article>A04</article>
</reference>
</references>

所以我需要在“-”处拆分每个乐器,以获得 id_celexarticle。 然后,对于每个唯一的 id_celex,我需要使用 id_celex 及其 article 来创建引用。

我昨天开始使用 xslt,但我已经卡住了:p

提前谢谢你!

我有几行,但由于它不起作用,我不确定显示它是否有用......

【问题讨论】:

  • 如果您的 XSLT 包含 xsl:for-each-group 指令,那么是的,显示它会很有用!

标签: xslt xslt-2.0


【解决方案1】:

看看:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

  <xsl:template match="instruments">
    <references>
      <xsl:for-each-group select="instrument" group-by="substring-before(text(),'-')">
        <reference>
          <id_celex>
            <xsl:value-of select="current-grouping-key()"/>
          </id_celex>
          <xsl:for-each select="current-group()">
            <article>
              <xsl:value-of select="substring-after(text(),'-')"/>
            </article>
          </xsl:for-each>
        </reference>
      </xsl:for-each-group>
    </references>
  </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 哇!非常感谢。我不知道 for-each-group 功能。我现在会试着理解你的代码:)
猜你喜欢
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
  • 2019-04-25
相关资源
最近更新 更多