我的原始XML
<?xml version="1.0" encoding="UTF-8"?>
<testing>
<MajorEvent>
<Originator>xxxx/Originator>
<Version>V1</Version>
</MajorEvent>
<Event Date="26-03-11_04:00:00:00" >
<AudioName>AT1</AudioName>
<ItemName>F7000533</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
</testing>
我想根据语言和audioname制作活动。例如,这里有3个事件,因此输出将是
<?xml version="1.0" encoding="UTF-8"?>
<testing>
<MajorEvent>
<Originator>xxxx/Originator>
<Version>V1</Version>
</MajorEvent>
<Event Date="26-03-11_04:00:00:00" >
<AudioName>AT1</AudioName>
<ItemName>F7000533</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533-AT1</ItemName>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00" >
<AudioName>AT1</AudioName>
<ItemName>F7000533-Language1</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00" >
<AudioName>AT1</AudioName>
<ItemName>F7000533-Language2</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
</testing>
我写了xslt。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl" >
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="/testing">
<xsl:element name="testing">
<xsl:apply-templates select="MajorEvent" />
<xsl:apply-templates select="Event" />
</xsl:element>
</xsl:template>
<xsl:template match="MajorEvent">
<xsl:element name="MajorEvent">
<xsl:copy-of select="Originator" />
<xsl:copy-of select="Version" />
</xsl:element>
</xsl:template>
<xsl:template match="Event">
<xsl:element name="Event">
<xsl:attribute name="Date">
<xsl:value-of select="@Date" />
</xsl:attribute>
<xsl:copy-of select="AudioName" />
<xsl:copy-of select="ItemName" />
<xsl:copy-of select="EventID" />
</xsl:element>
<xsl:for-each select="AudioName">
<xsl:element name="Event">
<xsl:attribute name="Date">
<xsl:value-of select="../@Date" />
</xsl:attribute>
<xsl:variable name="item_name" select="../ItemName" />
<xsl:variable name="audio" select="../AudioName" />
<xsl:copy-of select="../AudioName" />
<xsl:element name="ItemName">
<xsl:value-of select="concat($item_name,'-',$audio)" />
</xsl:element>
<xsl:copy-of select="../EventID" />
</xsl:element>
</xsl:for-each>
<xsl:variable name="SplitLanguage">
<xsl:call-template name="SplitLanguage">
<xsl:with-param name="Languages" select="Lang/Languages" />
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($SplitLanguage)/*">
<xsl:element name="Event">
<xsl:attribute name="Date">
<xsl:value-of select="Date" />
</xsl:attribute>
<xsl:variable name="item_name" select="../ItemName" />
<xsl:variable name="lang" select="." />
<xsl:element name="ItemName">
<xsl:value-of select="concat($item_name,'-',$lang)" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template name="SplitLanguage">
<xsl:param name="Languages" select="." />
<xsl:if test="string-length($Languages) >0">
<item>
<xsl:value-of select="substring-before(concat($Languages, ','), ',')" />
</item>
<xsl:call-template name="SplitLanguage">
<xsl:with-param name="Languages"
select="substring-after($Languages, ',')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我收到了这个结果
<?xml version="1.0" encoding="UTF-8"?>
<testing>
<MajorEvent>
<Originator>xxxx</Originator>
<Version>V1</Version>
</MajorEvent>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533</ItemName>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533-AT1</ItemName>
<EventID>4333</EventID>
</Event>
<Event Date="">
<ItemName>-Language1</ItemName>
</Event>
<Event Date="">
<ItemName>-Language2</ItemName>
</Event>
</testing>
有人知道我在这里做错了什么吗?
答案 0 :(得分:0)
我觉得你不需要exslt,你似乎也在分裂“,”而不是“|”
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<x>
<xsl:copy-of select="/"/>
<xsl:apply-templates select="testing/Event"/>
</x>
</xsl:template>
<xsl:template match="Event">
<Event Date="{@Date}">
<xsl:copy-of select="AudioName|ItemName|EventID"/>
</Event>
<xsl:call-template name="splitlang">
<xsl:with-param name="l" select="concat(Lang/Languages,'|')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="splitlang">
<xsl:param name="l"/>
<xsl:choose>
<xsl:when test="$l">
<Event Date="{@Date}">
<xsl:copy-of select="AudioName"/>
<ItemName>
<xsl:value-of select="ItemName"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring-before($l,'|')"/>
</ItemName>
<xsl:copy-of select="Lang|EventID"/>
</Event>
<xsl:call-template name="splitlang">
<xsl:with-param name="l" select="substring-after($l,'|')"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
产生
<x>
<testing>
<MajorEvent>
<Originator>xxxx</Originator>
<Version>V1</Version>
</MajorEvent>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
</testing>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533</ItemName>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533-Language1</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
<Event Date="26-03-11_04:00:00:00">
<AudioName>AT1</AudioName>
<ItemName>F7000533-Language2</ItemName>
<Lang>
<Languages>Language1|Language2</Languages>
</Lang>
<EventID>4333</EventID>
</Event>
</x>