开发者

Help with merging adjacent nodes

I apologize for not knowing xsl very well, but i have an xml document that I would like to transform and I've not been able to find an example that works for me. I would like to merge the locations into a single element. I have the following document:

<?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> 
    <locat开发者_开发百科ions> 
        <id>116</id> 
        <name>Athletic Complex</name> 
    </locations> 
    <locations> 
        <id>6</id> 
        <name>HS Baseball Field</name> 
    </locations>
</event>

I am trying to make it like this:

<?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>


This XSLT 1.0 transformation doesn't use modes and doesn't have even a single conditional instruction:

<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>

When applied on the provided XML document (wrapped in a single top element to be made well-formed)"

<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>

the wanted, correct result is produced:

<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>


Use the identity transform with templates that handle the special cases:

<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>


Here's an alternative (XSLT 2.0):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="event">
      <xsl:copy-of select="* except locations"/>
      <location_name>
        <xsl:value-of select="locations/name" separator=", "/>
      </location_name>
   </xsl:template>
</xsl:stylesheet>


EDIT: Sorry, I'd missed the sequence constructor.

<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>

Output:

<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>

Note: Pull style perform the transformation only when is needed, meaning when there is some locations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜