Police confirmed they are

Xml xslt question

I have an xml that has text inside "word" elements like

<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 xslt the text appears as "Policeconfirmedthey arequestioningaman".

Here is an xslt snippet that does this conversion

<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 show this as "The police have confirmed they are interrogating the person"?

Many thanks.

+1


source to share


5 answers


Add a whitespace character.

Simple way:

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

      



A slightly more complex way to effectively trim a 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.

+3


source


You can conditionally add space between words based on word position. The following snippet shows how to add a space after everything but the last word.

<xsl:template match="//words">
    <xsl:for-each select="word">
      <xsl:value-of select="."/>
      <!-- Check word position -->
      <xsl:if test="position()!=last()">
      <!-- Add a whitespace if it not the last word -->
        <xsl:text> </xsl:text>
      </xsl:if>
    </xsl:for-each>
</xsl:template>

      



If you are generating HTML output, you can use the information from this post to see how to add a space (instead of <xst: text> in my code snippet)

+4


source


it's almost like the answer "aku". But answering "aku" creates one paragraph.

 <xsl:for-each select="./speaker/segment">  
 <Paragraph>   
    <xsl:for-each select="./nbest/word"/>        
      <xsl:value-of select="."/>
        <xsl:if test="position()!=last()">
          <xsl:text> </xsl:text>
        </xsl:if>        
    </xsl:for-each>
 </Paragraph>
</xsl:for-each>

      

+2


source


The reason they work together is because the stylesheet uses a built-in pattern to match against which whitespace collapses. I don't see where you are explicitly calling <word/>

or the surrounding XML, so I am assuming this is calling <xsl:apply-templates />

somewhere. You just need to just define the match pattern as such:

<xsl:template match="word">
    <xsl:value-of select="." />&#160;
</xsl:template>

      

The whitespace is significant, so if you find in the above solution that it confuses everything, you can wrap it inside the <xsl:text/>

node and the whitespace will disappear.

Then when the word node is matched you get it from a distance. Note: there will be extra space at the end. To get rid of this, it will be slightly longer.

<xsl:temmplate match="word">
    <xsl:value-of select="." />
    <xsl:if test=". !=../word[last()]">
        <xsl:text>&#160;</xsl:text>
    </xsl:if>
</xsl:template> 

      

This will only work when applying templates, not when using values ​​or copies of xsl directives.

+2


source


try this adding & npsp; after tag value

0


source







All Articles