public string IntToString(int number)//nobody really uses negative numbers
{
if(number == 0)
return "zero";
else
if(number == 1)
return "one";
.......
else
if(number == 2147483647)
return "two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven";
}
Это легко сделать, если верно следующее (как я предполагаю):
s в пределах
уникальны
s сразу после данного
принадлежат ему
без элемента
перед ним Это решение XSLT 1.0:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:key name="kFixture"
match="fixture"
use="generate-id(preceding-sibling::timeline[1])"
/>
<xsl:template match="data">
<xsl:copy>
<xsl:apply-templates select="competition" />
</xsl:copy>
</xsl:template>
<xsl:template match="competition">
<xsl:copy>
<xsl:apply-templates select="timeline" />
</xsl:copy>
</xsl:template>
<xsl:template match="timeline">
<xsl:copy>
<xsl:attribute name="time">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:copy-of select="key('kFixture', generate-id())" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
производит:
<data>
<competition>
<timeline time="10:00">
<fixture>team a v team b</fixture>
<fixture>team c v team d</fixture>
</timeline>
<timeline time="12:00">
<fixture>team e v team f</fixture>
</timeline>
<timeline time="16:00">
<fixture>team g v team h</fixture>
<fixture>team i v team j</fixture>
<fixture>team k v team l</fixture>
</timeline>
</competition>
</data>
Обратите внимание на использование
для соответствия всем
s, которые принадлежат ("предшествуют") заданному
.
Немного более коротким, но менее очевидным решением было бы модифицированное преобразование идентичности:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:key name="kFixture"
match="fixture"
use="generate-id(preceding-sibling::timeline[1])"
/>
<xsl:template match="* | @*">
<xsl:copy>
<xsl:apply-templates select="*[not(self::fixture)] | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="timeline">
<xsl:copy>
<xsl:attribute name="time">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:copy-of select="key('kFixture', generate-id())" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Вот моя попытка. Одно предположение, которое я сделал, которое упрощает ситуацию, заключается в том, что элементы шкалы времени с определенным текстовым значением уже уникальны.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="/data">
<data>
<xsl:apply-templates select="competition" />
</data>
</xsl:template>
<xsl:template match="competition">
<xsl:for-each select="timeline">
<timeline time="{text()}">
<xsl:copy-of
select="./following-sibling::fixture[count(preceding-sibling::timeline[1] | current()) = 1]" />
</timeline>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Вышесказанное отредактировано для использования current () вместо переменной согласно предложению Томалака.
Решение Андриё не работает, так как, к сожалению, нет таких осей, как «следующий брат».
И альтернативное решение было бы следующее:
<xsl:template match="timeline">
<timeline>
<xsl:attribute name="time" >
<xsl:value-of select="." />
</xsl:attribute>
<xsl:apply-templates select="following-sibling::*[local-name()='fixture' and position()=1]" />
</timeline>
</xsl:template>
<xsl:template match="fixture">
<fixture>
<xsl:value-of select="." />
</fixture>
<xsl:apply-templates select="following-sibling::*[local-name()='fixture' and position()=1]" />
</xsl:template>
Попробуйте что-нибудь вроде этого:
<xsl:template match="timeline">
<timeline>
<xsl:attribute name="time" >
<xsl:value-of select="." />
</xsl:attribute>
<xsl:apply-templates select="following-sibling::*[name()=fixture][1]" />
</timeline>
</xsl:template>
<xsl:template match="fixture">
<fixture>
<xsl:value-of select="." />
</fixture>
<xsl:apply-templates select="following-sibling::*[name()=fixture][1]" />
</xsl:template>
С помощью g andrieu Мне пришлось заставить его получить только следующий элемент, а не следующий список:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="competition" >
<xsl:apply-templates select="timeline" />
</xsl:template>
<xsl:template match="timeline">
<timeline>
<xsl:attribute name="time" >
<xsl:value-of select="." />
</xsl:attribute>
<xsl:apply-templates select="following-sibling::*[1]" mode="copy"/>
</timeline>
</xsl:template>
<xsl:template match="fixture" mode="copy">
<fixture>
<xsl:value-of select="." />
</fixture>
<xsl:apply-templates select="following-sibling::*[1]" mode="copy"/>
</xsl:template>
<xsl:template match="timeline" mode="copy" />
</xsl:stylesheet>
Следующий xslt будет работать, даже если одинаковые временные шкалы разбросаны в нескольких местах. Например, в foll xml есть 2 записи для временной шкалы 10:00
<?xml version="1.0" encoding="UTF-8"?>
<data>
<competition>
<timeline>10:00</timeline>
<fixture>team a v team b</fixture>
<fixture>team c v team d</fixture>
<timeline>12:00</timeline>
<fixture>team e v team f</fixture>
<timeline>16:00</timeline>
<fixture>team g v team h</fixture>
<fixture>team i v team j</fixture>
<fixture>team k v team l</fixture>
<timeline>10:00</timeline>
<fixture>team a v team b new</fixture>
<fixture>team c v team d new</fixture>
</competition>
</data>
Xslt :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="TimelineDistint" match="timeline" use="."/>
<xsl:template match="data">
<xsl:apply-templates select="competition"/>
</xsl:template>
<xsl:template match="competition">
<data>
<competition>
<xsl:for-each select="timeline[generate-id() = generate-id(key('TimelineDistint', .)[1])]">
<timeline>
<xsl:variable name="varTimeline" select="."/>
<xsl:attribute name="time"><xsl:value-of select="normalize-space(.)"/></xsl:attribute>
<xsl:for-each select="../fixture[preceding::timeline[1] = $varTimeline]">
<fixture>
<xsl:value-of select="normalize-space(.)"/>
</fixture>
</xsl:for-each>
</timeline>
</xsl:for-each>
</competition>
</data>
</xsl:template>
</xsl:stylesheet>
Выход :
<?xml version="1.0" encoding="UTF-8"?>
<data>
<competition>
<timeline time="10:00">
<fixture>team a v team b</fixture>
<fixture>team c v team d</fixture>
<fixture>team a v team b new</fixture>
<fixture>team c v team d new</fixture>
</timeline>
<timeline time="12:00">
<fixture>team e v team f</fixture>
</timeline>
<timeline time="16:00">
<fixture>team g v team h</fixture>
<fixture>team i v team j</fixture>
<fixture>team k v team l</fixture>
</timeline>
</competition>
</data>