2011-04-12 83 views
4

我很抱歉无法很好地了解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> 

+0

问得好,+1。查看我的答案,找到不使用任何模式或任何XSLT条件指令的解决方案。 :) – 2011-04-13 03:50:27

回答

1

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

使用身份转换与处理特殊情况的模板:

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

编辑:对不起,我错过了顺序构造函数。

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

注意:拉风进行改造只有当需要,当有一些locations意义。

0

这里的一个替代(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> 
+0

感谢所有,很好的答案。 – Derbium 2011-04-13 14:48:31