开发者

Sort two different XML-elements together and display them

I got a XML-document with several values, this is part of it:

<result>
  <element>
    <title>Title 1</title>
    <alternative>
      <alternativeTitle>Title 5<alternativeTitle>
    </alternative>
    <data>
      ...
    </data>
  </element>

  <element>
    <title>Title 3</title>
    <alternative>
      <alternativeTitle>Title 2<alternativeTitle>
      <alternativeTitle>Title 4<alternativeTitle>
    </alternative>
    <data>
      ...
    </data>
  </element>
</result>

What I'm trying to accomplish is to sort each title, including the alternative titles. This means that the two elements above would produce five elements that should be sorted (result should have been Title 1, Title 2, Title 3, Title 4, Title 5). It's also important that the data is accessible after the sort, so that it's possible to make a list and present some of data as well. Any ideas on how to do that?

In advance, I'd like to thank you for your help!

EDIT: Sorry for not specifying my desired output at the time of creating this question. The structure is a set of articles (title-element is the title of the article and so on), I want to present them in a list which is sorted. It might seem counterintuitive to sort both title and alternative titles, but it has its purpose. :)

By using XSL, we produce an XHTML that will be displayed to the user. It will look something like开发者_运维技巧:

TITLE 1 (which is also a link to the full article)
some data from title 1
TITLE 2 (which is also a link to the full article)
some data from title 2
TITLE 3 (which is also a link to the full article)
some data from title 3

and so on..

In this manner, articles are listed several times, but with different titles. I hope this clarifies things, and thanks again for the effort so far! I am unable to try Simon's answer at the moment, but I guess I can tweek it to get my desired output?


I've corrected/completed your XML a bit, to the following:

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

<result>
  <element>
    <title>Title 1</title>
    <alternative>
      <alternativeTitle>Title 5</alternativeTitle>
    </alternative>
    <data>
      <foo>123</foo>
    </data>
  </element>
  <element>
    <title>Title 3</title>
    <alternative>
      <alternativeTitle>Title 2</alternativeTitle>
      <alternativeTitle>Title 4</alternativeTitle>
    </alternative>
    <data>
        <baz>321</baz>
    </data>
  </element>
</result>

It would have been more helpful if you'd provided an example of the output you were hoping for, but I think the following should do that you're asking:

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

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <xsl:apply-templates select="result" />
    </xsl:template>

    <xsl:template match="result">
        <entries>
            <xsl:apply-templates select="element/title|element/alternative/alternativeTitle">
                <xsl:sort select="string(.)" />
            </xsl:apply-templates>
        </entries>
    </xsl:template>

    <xsl:template match="title">
        <entry>
            <xsl:copy-of select="." />
            <xsl:copy-of select="../data" />
        </entry>
    </xsl:template>

    <xsl:template match="alternative/alternativeTitle">
        <entry>
            <title><xsl:value-of select="(.)" /></title>
            <xsl:copy-of select="../../data" />
        </entry>
    </xsl:template>
</xsl:stylesheet>

This produces output (given the amended sample) of:

<?xml version="1.0"?> 
<entries> 
  <entry> 
    <title>Title 1</title> 
    <data> 
      <foo>123</foo> 
    </data> 
  </entry> 
  <entry> 
    <title>Title 2</title> 
    <data> 
        <baz>321</baz> 
    </data> 
  </entry> 
  <entry> 
    <title>Title 3</title> 
    <data> 
        <baz>321</baz> 
    </data> 
  </entry> 
  <entry> 
    <title>Title 4</title> 
    <data> 
        <baz>321</baz> 
    </data> 
  </entry> 
  <entry> 
    <title>Title 5</title> 
    <data> 
      <foo>123</foo> 
    </data> 
  </entry> 
</entries>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜