0

I have this XML file:

<produce>
    <item>apple</item>
    <item>banana</item>
    <item>pepper</item>
    <item>apples</item>
    <item>pizza</item>
</produce>

and I want extract only items' name , for example apple,banana,pepper,apples and pizza, for this reason I create this XSL file:

  <?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
    <body>
    <ul>
    <li><xsl:value-of select="text()"/></li>
    </ul>
    </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

but I don't understand why it doesn't work. Maybe I don't understand how works text() function. Can you help me?

gino gino
  • 249
  • 1
  • 4
  • 8

1 Answers1

0

Currently you are outputting on li for the current node, which is the top-level document node. The text() function will get the text node that is the immediate child of the current node. But in your XML the text nodes you want are children of item elements.

If you want an li for each item in your XML, you first need to select those item elements, using either xsl:for-each or xsl:apply-templates.

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
    <body>
    <ul>
    <xsl:for-each select="produce/item">
        <li><xsl:value-of select="text()"/></li>
    </xsl:for-each>
    </ul>
    </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • sorry the "/" represent which node ? – gino gino Jan 18 '16 at 11:38
  • It effectively represents the XML document. It is the parent of the `produce` node in your case. – Tim C Jan 18 '16 at 11:46
  • See http://stackoverflow.com/questions/3127108/xsl-xsltemplate-match for a full explanation. – Tim C Jan 18 '16 at 11:48
  • @ginogino `/*` is neither in your question nor in this answer. `/*` means the outermost element of an XML document - `produce` in your case, but the `*` means the element could have any name. If you have more unrelated questions, please accept this answer and then ask a new question. – Mathias Müller Jan 18 '16 at 11:56