Search code examples
xmlxslt

xml xslt question


I have an xml that has text within "word" elements e.g

<word>Police</word>
<word>confirmed</word>
<word>they are</word>
<word>questioning</word>
<word>a</word>
<word>man</word>

The problem is when I apply the xslt the text appears like "Policeconfirmedthey arequestioningaman".

Here is the xslt snippet that does this transform

<Paragraph>
 <xsl:for-each select="./speaker/segment">
 <xsl:value-of select="./nbest"/>
 </xsl:for-each>
</Paragraph>

Can anyone offer any help as to how I can display this as "Police confirmed they are questioning a man"?

Many thanks.


Solution

  • Add a space character.

    Simple way:

    <Paragraph>
     <xsl:for-each select="./speaker/segment">
       <xsl:value-of select="./nbest"/>&#160;
     </xsl:for-each>
    </Paragraph>
    

    Slightly more complex way to effectively trim the string:

    <Paragraph>
     <xsl:for-each select="./speaker/segment">
       <xsl:if test="not(position() = 1)">&#160;</xsl:if>
       <xsl:value-of select="./nbest"/>
     </xsl:for-each>
    </Paragraph>
    

    And some xpath ways: normalize-space, substring-before, substring-after in various forms.