Xml 帮助合并相邻节点
很抱歉,我不太了解xsl,但我有一个xml文档要转换,我还没有找到一个适合我的示例。我想将这些位置合并到一个元素中。我有以下文件:Xml 帮助合并相邻节点,xml,xslt,Xml,Xslt,很抱歉,我不太了解xsl,但我有一个xml文档要转换,我还没有找到一个适合我的示例。我想将这些位置合并到一个元素中。我有以下文件: <?xml version="1.0" encoding="UTF-8"?><tfs_events> <title>Referees Events</title> <event> <id>256</id> <name>SB-V,SB-JV vs M
<?xml version="1.0" encoding="UTF-8"?><tfs_events>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<locations>
<id>116</id>
<name>Lake Athletic Complex</name>
</locations>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<locations>
<id>116</id>
<name>Athletic Complex</name>
</locations>
<locations>
<id>6</id>
<name>HS Baseball Field</name>
</locations>
</event>
裁判员
256
SB-V,SB-JV对麦金利HS
2011-04-12 17:00:00
2011-04-12 19:00:00
活跃的
116
湖滨体育中心
257
SB-V,SB-JV对杰克逊HS
2011-04-14 17:00:00
2011-04-14 19:00:00
活跃的
116
运动综合体
6.
HS棒球场
我正试着这样做:
<?xml version="1.0" encoding="UTF-8"?><tfs_events>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<location_name>Lake Athletic Complex</location_name>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<location_name>Athletic Complex, HS Baseball Field</location_name>
</event>
裁判员
256
SB-V,SB-JV对麦金利HS
2011-04-12 17:00:00
2011-04-12 19:00:00
活跃的
湖滨体育中心
257
SB-V,SB-JV对杰克逊HS
2011-04-14 17:00:00
2011-04-14 19:00:00
活跃的
体育综合体,HS棒球场
将身份转换与处理特殊情况的模板一起使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="event">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::locations)]" />
<location_name>
<xsl:apply-templates select="locations" />
</location_name>
</xsl:copy>
</xsl:template>
<xsl:template match="locations">
<xsl:value-of select="name" />
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
,
编辑:对不起,我错过了序列构造函数
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="sequence">
<xsl:if test="position()!=1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="locations"/>
<xsl:template match="locations[1]">
<location_name>
<xsl:apply-templates select="../locations/name" mode="sequence"/>
</location_name>
</xsl:template>
</xsl:stylesheet>
,
输出:
<tfs_events>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<location_name>Lake Athletic Complex</location_name>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<location_name>Athletic Complex, HS Baseball Field</location_name>
</event>
</tfs_events>
裁判员
256
SB-V,SB-JV对麦金利HS
2011-04-12 17:00:00
2011-04-12 19:00:00
活跃的
湖滨体育中心
257
SB-V,SB-JV对杰克逊HS
2011-04-14 17:00:00
2011-04-14 19:00:00
活跃的
体育综合体,HS棒球场
注意:拉式样式仅在需要时执行转换,这意味着当存在一些位置时
这里有一个替代方法(XSLT 2.0):
此XSLT 1.0转换不使用模式,甚至没有单个条件指令:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="locations[1]">
<location_name>
<xsl:apply-templates select=
"name | following-sibling::locations/name"/>
</location_name>
</xsl:template>
<xsl:template match="locations"/>
<xsl:template priority="5" match=
"locations[preceding-sibling::locations]/name">
<xsl:value-of select="concat(', ', .)"/>
</xsl:template>
<xsl:template match="locations/name[1]">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时(包装在一个顶部元素中,以使其格式良好)
裁判员
256
SB-V,SB-JV对麦金利HS
2011-04-12 17:00:00
2011-04-12 19:00:00
活跃的
116
湖滨体育中心
257
SB-V,SB-JV对杰克逊HS
2011-04-14 17:00:00
2011-04-14 19:00:00
活跃的
116
运动综合体
6.
HS棒球场
生成所需的正确结果:
<t>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<location_name>Lake Athletic Complex</location_name>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<location_name>Athletic Complex, HS Baseball Field</location_name>
</event>
</t>
裁判员
256
SB-V,SB-JV对麦金利HS
2011-04-12 17:00:00
2011-04-12 19:00:00
活跃的
湖滨体育中心
257
SB-V,SB-JV对杰克逊HS
2011-04-14 17:00:00
2011-04-14 19:00:00
活跃的
体育综合体,HS棒球场
好问题,+1。请参阅我的答案,了解不使用任何模式或任何XSLT条件指令的解决方案。:)
<t>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<locations>
<id>116</id>
<name>Lake Athletic Complex</name>
</locations>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<locations>
<id>116</id>
<name>Athletic Complex</name>
</locations>
<locations>
<id>6</id>
<name>HS Baseball Field</name>
</locations>
</event>
</t>
<t>
<title>Referees Events</title>
<event>
<id>256</id>
<name>SB-V,SB-JV vs McKinley HS</name>
<time_start>2011-04-12 17:00:00</time_start>
<time_end>2011-04-12 19:00:00</time_end>
<status>Active</status>
<location_name>Lake Athletic Complex</location_name>
</event>
<event>
<id>257</id>
<name>SB-V,SB-JV vs Jackson HS</name>
<time_start>2011-04-14 17:00:00</time_start>
<time_end>2011-04-14 19:00:00</time_end>
<status>Active</status>
<location_name>Athletic Complex, HS Baseball Field</location_name>
</event>
</t>