2014-04-17 30 views
0

首先,我对XSLT不太好。有人可以帮我弄这个吗?xslt连接字段加回

如何连接节点中的两个字段,然后将其作为新字段添加到原始节点?例如,

<Contacts> 
<Contact> 
    <fName>Mickey</fName> 
    <lName>Mouse</lName> 
</Contact> 
<Contact> 
<fName>Daisy</fName> 
<lName>Duck</lName> 
</Contact> 
</Contacts> 

<Contacts> 
    <Contact> 
    <fName>Mickey</fName> 
    <lName>Mouse</lName> 
    <fullName>MickeyMouse</fullName> 
</Contact> 
<Contact> 
    <fName>Daisy</fName> 
    <lName>Duck</lName> 
    <fullName>DaisyDuck</fullName> 
    </Contact> 
</Contacts> 

在此先感谢,

回答

1

你想复制与-变化。这在XSLT中通过一个标识模板开始,可以完全复制节点,然后在需要变体的地方覆盖它。

下变换

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes" /> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Contact"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
      <fullName><xsl:value-of select="concat(fName, lName)"/></fullName> 
     </xsl:copy> 
    </xsl:template> 

</xsl:transform> 

应用到你的输入,给人的通缉的结果:

<Contacts> 
    <Contact> 
     <fName>Mickey</fName> 
     <lName>Mouse</lName> 
    <fullName>MickeyMouse</fullName></Contact> 
    <Contact> 
     <fName>Daisy</fName> 
     <lName>Duck</lName> 
    <fullName>DaisyDuck</fullName></Contact> 
</Contacts> 

希望这有助于。

+0

甜。谢谢。 – gishman

0

您创建一个模板来匹配联系人,就像这样:

<xsl:template match="Contact"> 
    <Contact> 
    <xsl:copy-of select="*"/> 
    <fullName><xsl:value-of select="concat(fName, ' ',lName)"/></fullName> 
    </Contact> 
</xsl:template>