当前位置 : 主页 > 网页制作 > xml >

xml – 帮助合并相邻节点

来源:互联网 收集:自由互联 发布时间:2021-06-13
我为不太了解xsl而道歉,但我有一个我想要转换的xml文档,而且我找不到适合我的示例.我想将位置合并为一个元素.我有以下文件: ?xml version="1.0" encoding="UTF-8"?tfs_events titleReferees Events/ti
我为不太了解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 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>

我想这样做:

<?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>
此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文档时(包装在单个顶部元素中以使格式良好)

<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>
网友评论