2012-07-10 55 views
2

我有下面的XML文档:XSLT逗号元素脱离

<?xml version="1.0" encoding="UTF-8"?> 
<cars> 
    <car> 
     <entrydata columnnumber="4" name="Colour"> 
      <text>Red</text> 
     </entrydata> 
    </car> 
    <car> 
     <entrydata columnnumber="4" name="Colour"> 
      <textlist> 
       <text>Yellow</text> 
       <text>Blue</text> 
      </textlist> 
     </entrydata> 
    </car> 
</cars> 

和以下XSLT样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
    <records> 
     <xsl:apply-templates select="//cars"/> 
    </records> 
    </xsl:template> 
    <!--Top level template --> 
    <xsl:template match="cars"> 
    <!-- Loop through each document (viewentry) and apply create the rows for each one--> 
    <xsl:for-each select="car"> 
     <record> 
     <xsl:attribute name="Colour"> 
      <xsl:value-of select="entrydata[@name='Colour']"/> 
     </xsl:attribute> 
     </record> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

这将产生以下输出:

<?xml version="1.0" encoding="UTF-8"?> 
<records> 
    <record Colour="Red"/> 
    <record Colour="YellowBlue"/> 
</records> 

我将如何修改XSLT文件,以便输出变为(注意<textlist>逗号分隔):

<?xml version="1.0" encoding="UTF-8"?> 
<records> 
    <record Colour="Red"/> 
    <record Colour="Yellow, Blue"/> 
</records> 
+0

这是一个起点:http://www.xsltcake.com/slices/t4nxk9/2 – joshcomley 2012-07-10 09:52:59

回答

2

一个不那么冗长,纯粹的“推式” 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="cars"> 
    <records> 
     <xsl:apply-templates/> 
    </records> 
</xsl:template> 
<xsl:template match="car"> 
    <record> 
    <xsl:apply-templates/> 
    </record> 
</xsl:template> 
<xsl:template match="entrydata"> 
    <xsl:attribute name="{@name}"> 
    <xsl:apply-templates/> 
    </xsl:attribute> 
</xsl:template> 
<xsl:template match="text"> 
    <xsl:if test="position() >1">, </xsl:if> 
    <xsl:value-of select="."/> 
</xsl:template> 
</xsl:stylesheet> 

当这个变换所提供的XML文档应用:

<cars> 
    <car> 
     <entrydata columnnumber="4" name="Colour"> 
      <text>Red</text> 
     </entrydata> 
    </car> 
    <car> 
     <entrydata columnnumber="4" name="Colour"> 
      <textlist> 
       <text>Yellow</text> 
       <text>Blue</text> 
      </textlist> 
     </entrydata> 
    </car> 
</cars> 

的想要的,正确的结果产生

<records> 
    <record Colour="Red"/> 
    <record Colour="Yellow, Blue"/> 
</records> 

说明

正确使用的模板,模式匹配,AVTS和position()功能。


二,更简单的XSLT 2。0溶液

<xsl:stylesheet version="2.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="cars"> 
    <records> 
     <xsl:apply-templates/> 
    </records> 
</xsl:template> 
<xsl:template match="car"> 
    <record> 
    <xsl:apply-templates/> 
    </record> 
</xsl:template> 
<xsl:template match="entrydata"> 
    <xsl:attribute name="{@name}"> 
    <xsl:value-of select=".//text" separator=", "/> 
    </xsl:attribute> 
</xsl:template> 
</xsl:stylesheet> 

当这种转化是在同一个XML文档(上图)中,相同的正确的结果产生施加:

<records> 
    <record Colour="Red"/> 
    <record Colour="Yellow, Blue"/> 
</records> 

说明

正确使用的模板,模式匹配,AVT和的separator属性

2

使用XSLT 2.0,您可以使用

<record Colour="{string-join(entrydata[@name='Colour']/textlist/text, ', ')}"/> 

使用XSLT 1.0,我会做

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
    <records> 
     <xsl:apply-templates select="//cars"/> 
    </records> 
    </xsl:template> 
    <!--Top level template --> 
    <xsl:template match="cars/car"> 
    <record> 
     <xsl:attribute name="Colour"> 
      <xsl:apply-templates select="entrydata[@name='Colour']textlist/text"/> 
     </xsl:attribute> 
     </record> 
    </xsl:template> 
    <xsl:template match="textlist/text"> 
    <xsl:if test="position() > 1"> 
     <xsl:text>, </xsl:text> 
    </xsl:if> 
    <xsl:value-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 
+0

AVT也可用于XSLT 1.0。 – 2012-07-10 10:26:23

+0

肖恩,如果你想要逗号分隔的几个节点值的列表,单独的属性值模板不会有帮助。相反,使用XSLT/XPath 1.0语义执行''会输出第一个'text'元素的值,而不是所有。 – 2012-07-10 10:39:06

+0

请参阅我使用AVT的解决方案。我测试过了。有用。 – 2012-07-10 10:41:27

1

这XSLT 1.0样式表就可以了...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
    <records> 
     <xsl:apply-templates select="*/*"/> 
    </records> 
    </xsl:template> 

    <xsl:template match="car"> 
    <xsl:variable name="colour-list"> 
    <xsl:for-each select="entrydata[@name='Colour']/text | 
          entrydata[@name='Colour']/textlist/text"> 
     <xsl:value-of select="concat(.,', ')" /> 
    </xsl:for-each> 
    </xsl:variable> 
     <record Colour="{substring($colour-list,1,string-length($colour-list)-2)}"/> 
    </xsl:template> 
</xsl:stylesheet> 

但对于简单的解决赢家是这样的XSLT 2.0样式表...

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/"> 
<records> 
    <xsl:apply-templates select="*/*"/> 
</records> 
</xsl:template> 

<xsl:template match="car"> 
<record Colour="{string-join(entrydata[@name='Colour']/(text | textlist/text),', ')}"/> 
</xsl:template> 
</xsl:stylesheet>