【问题标题】:XSLT 1.0 For-Each-Group flat XMLXSLT 1.0 For-Each-Group 平面 XML
【发布时间】:2019-05-16 09:55:04
【问题描述】:

我需要一些帮助。我对 XSLT 有点陌生。

我知道在 2.0 中您可以使用 For-Each-Group 来解决我的问题,但我仅限于 1.0。

我需要使用“group-starting-with”之类的功能对平面 XML 进行分组。

这只是一个例子,但我的实际问题非常相似。

我有这个 XML:

<?xml version="1.0" encoding="UTF-8"?>
    <catalog>

        <xpto name="1">ABC</xpto>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
        <xpto name="2">ABC</xpto>

        <xpto name="1">ABC</xpto>
        <title>Hide your heart</title>
        <artist>Bob Dylan</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
        <xpto name="2">ABC</xpto>

    </catalog>

我希望它是:

<?xml version="1.0" encoding="UTF-8"?>
    <catalog>

        <group>
            <xpto name="1">ABC</xpto>
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>USA</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
            <xpto name="2">ABC</xpto>
        </group>

        <group>
            <xpto name="1">ABC</xpto>
            <title>Hide your heart</title>
            <artist>Bob Dylan</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
            <xpto name="2">ABC</xpto>
        </group>

    </catalog>

所以我想在每次出现以下内容时对元素进行分组:

    <xpto name="1">ABC</xpto>

有没有办法用 XSLT 1.0 做到这一点?

非常感谢!

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow!你能提供你迄今为止尝试过的东西吗?

标签: xml xslt xslt-1.0 xslt-grouping


【解决方案1】:

假设您想对以 &lt;xpto name="1"&gt; 元素开头的元素进行分组,您可以定义一个键,以按其前面的第一个此类元素对其他子元素进行分组:

 <xsl:key name="start" match="*[not(self::xpto[@name='1'])]" use="generate-id(preceding-sibling::xpto[@name='1'][1])" />

然后,您可以选择所有起始元素,然后像这样获取其他组项:

<xsl:apply-templates select=".|key('start', generate-id())" /> 

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:key name="start" match="*[not(self::xpto[@name='1'])]" use="generate-id(preceding-sibling::xpto[@name='1'][1])" />

  <xsl:output method="xml" indent="yes" />

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

  <xsl:template match="catalog">
    <xsl:copy>
      <xsl:for-each select="xpto[@name='1']">
        <group>
          <xsl:apply-templates select=".|key('start', generate-id())" /> 
        </group>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

【讨论】:

  • 亲爱的 Tim C,非常感谢。解决了我的问题!!我不能投票,因为我的个人资料是新的,stackoverflow 不允许:(
  • @Kaisers :您可以通过接受答案来关闭问题
  • 玩了一会儿之后我需要你的帮助。添加组标签后,我需要循环每个组并获取艺术家的价值。我怎么能做到这一点?我正在为模板逻辑而苦苦挣扎......
  • @Kaisers,提出一个包含必要细节的新问题。在您最初的问题中,您想输出组的所有元素,蒂姆向您展示了如何做到这一点(以及如何使用.|key('start', generate-id()) 选择组中的所有项目。目前尚不清楚为什么您认为需要“循环”或您在哪里/如何需要该特定值。提出一个新问题来解释这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多