Search code examples
xmlxsltvalue-of

What expression to use with xsl:value-of to select text value from certain elements


I have this kind of XML structure and I need to print out what both paragraph sections contains. How to do that? Basically I thought about for-each loop, but what to put inside xsl:value-of construction? Thanks!

   <slideshow>
        <slide id="A1">
            <title>XML techniques</title>
            <paragraph> Slideshow prepresents different kind of <bold>XML</bold> techniques </paragraph>
            <paragraph> Most common XML Techniques are </paragraph>

Solution

  • Assuming your XSLT looks something like

    <xsl:for-each select="//paragraph">
      ???
    </xsl:for-each>
    

    You could write:

    <xsl:for-each select="//paragraph">
      <xsl:copy-of select="node()"/>
    </xsl:for-each>
    

    ... this would return a copy of the nodes - text and elements - that are children of the paragraph.

    Depending on what other rules you have and want to execute, you could also write:

    <xsl:for-each select="//paragraph">
      <xsl:apply-templates select="node()"/>
    </xsl:for-each>
    

    ... this would also return a copy of the nodes - text and elements - that are children of the paragraph, unless you've got other templates overriding that behaviour.

    If all you wanted was the raw text in each paragraph (i.e. without the bold tags), you could use a value-of.

    <xsl:for-each select="//paragraph">
      <xsl:value-of select="."/>
    </xsl:for-each>
    

    If that's all you're doing, you could even write it as:

    <xsl:value-of select="//paragraph"/>
    

    (Note: I give //paragraph as an example because no context is provided but probably you want to be going through the slides and selecting the paragraph children).