2014-04-17 35 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>