Domanda

I wonder if there's way (actually I tried to find myself but hasn't been successful at all) how to re-sort XML report of records according to some group chracteristics. Let's say I have log file consisting of records in form of

<?xml version="1.0"?>
<log>
  <record id="12345">
    <author>me</author>
    <ts>2014-05-02T13:03:50.528995Z</ts>
    <data>arbitrary data</data>
  </record>
  <record id="23456">
    <author>someone else</author>
    <ts>2014-05-05T08:24:32.123495Z</ts>
    <data>another arbitrary data</data>
  </record>
  …
</log>

What I want is to group records by author in a report: author's name and below ordered list of particular records. I don't know in advance who authors are, so I need to create list of their names dynamically.

Is XSLT capable of solving of this?

È stato utile?

Soluzione

OK, so after carefully reading Jeni Tennison's tutorial I've figured it out.

<xsl:stylesheet version="1.0">

  <xsl:key name="key-authors" match="record" use="author" />

  <xsl:template match="log">

    <xsl:for-each select="record[generate-id() = generate-id(key('kAuthors',author)[1])]">
      <xsl:sort select="./author" />
      <xsl:value-of select="./author" />

      <xsl:for-each select="key('kAuthors',./author)">
        <xsl:apply-templates select="."/>
      </xsl:for-each>

    </xsl:for-each>

  </xsl:template>

  <xsl:template match="record">
    <!-- do whatever with the record values: @id, author, ts, data -->
  </xsl:template>

</xsl:stylesheet>

Thanks, michael.hor257k.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top