如何用XSL合并XML的Name与Surname节点生成新节点
Fixing XSLT to Combine Name and Surname with a Space
Your current XSLT is missing a space between the Name and Surname values, which is why you’re getting combined text without a gap (like JanKowalski instead of Jan Kowalski). Here’s how to fix it:
Corrected XSLT Code
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="ClientList/Client"> <xsl:element name="NameSurname"> <!-- Option 1: Explicitly add a space using xsl:text --> <xsl:value-of select="Name"/> <xsl:text> </xsl:text> <xsl:value-of select="Surname"/> <!-- Alternatively, use concat() for a more concise approach: <xsl:value-of select="concat(Name, ' ', Surname)"/> --> </xsl:element> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Breakdown of the Fix:
- Option 1: Using
<xsl:text> </xsl:text>guarantees a single space between the first and last name. This is explicit and avoids accidental whitespace issues from line breaks in your code. - Option 2: The
concat()function combines theNamevalue, a space string, and theSurnamevalue into one unified string, which is output directly. This is a cleaner way to handle concatenation in a single line.
Input XML
<ClientList> <Client> <Name>Jan</Name> <Surname>Kowalski</Surname> </Client> </ClientList>
Expected Output XML
<NameSurname>Jan Kowalski</NameSurname>
Either approach will give you the desired formatted name with the proper space between components.
内容的提问来源于stack exchange,提问作者Name
相关产品推荐
相关产品推荐

