2

I have following type of file contains in xml format

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <serviceImpl category="default">
        <package>esterMemoryManagement</package>
        <service singleton="true">
            <provides>xoc.hw.cor.memgt.ZContentType</provides>
            <brief>This sis first sdrevice</brief>
        </service>
    </serviceImpl>
    <serviceImpl category="default">
        <package>w.cor.TesterM</package>
        <service singleton="true">
            <provides>xoc.hw.ZAccessTypeProvid</provides>
            <brief>This sis first sdrevice</brief>
        </service>
    </serviceImpl>
</root>

i have to get all values within tag <provides></provides> in .xsl file. How can i do that? Thanks in advance.

Phillip Kovalev
  • 2,479
  • 22
  • 23
balaji
  • 1,075
  • 3
  • 12
  • 26
  • I can't see any XML in your question. Additionally, it's totally unclear what you want to do. I strongly recommend that you rephrase the question, or remove it. – MarcoS May 11 '11 at 09:30
  • 1
    @MarcoS, looking via the `|edit|` button helps :-) – rsp May 11 '11 at 10:45
  • Good question, +1. See my answer for the shortest solution that is also completely "push style". – Dimitre Novatchev May 12 '11 at 03:17

3 Answers3

1

Here is a short and complete solution:

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

 <xsl:template match="provides">
  <xsl:value-of select="concat(.,'&#xA;')"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<root>
    <serviceImpl category="default">
        <package>esterMemoryManagement</package>
        <service singleton="true">
            <provides>xoc.hw.cor.memgt.ZContentType</provides>
            <brief>This sis first sdrevice</brief>
        </service>
    </serviceImpl>
    <serviceImpl category="default">
        <package>w.cor.TesterM</package>
        <service singleton="true">
            <provides>xoc.hw.ZAccessTypeProvid</provides>
            <brief>This sis first sdrevice</brief>
        </service>
    </serviceImpl>
</root>

the wanted, correct result is produced:

xoc.hw.cor.memgt.ZContentType
xoc.hw.ZAccessTypeProvid

Explanation:

  1. The only template that produces the result is the one matching provides.

  2. The second template matches any text node and has an empty body, which effectively overrides the XSLT built-in template for text nodes and prevents ("deletes") any matched text node from being output (an action that otherwise would have been performed by the XSLT built-in template).

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

Here's one way of doing it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="text"/>

    <xsl:template match="root">
        <xsl:apply-templates select="serviceImpl"/>
    </xsl:template>

    <xsl:template match="serviceImpl">
        <xsl:apply-templates select="service"/>
        <xsl:text>,</xsl:text>
    </xsl:template>

    <xsl:template match="service">
        <xsl:apply-templates select="provides"/>
    </xsl:template>

    <xsl:template match="provides">
        <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>

You may also want to have a look at this question, and related answers.

Community
  • 1
  • 1
MarcoS
  • 13,386
  • 7
  • 42
  • 63
0

You can use XSL to filter out values like:

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

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="//serviceImpl/provides" />
  </xsl:template>

  <xsl:template match="serviceImpl/provides">
    <xsl:value-of select="text()" />
    <xsl:text>&#x0A;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

(btw, Your example XML is not correct in all places)

rsp
  • 23,135
  • 6
  • 55
  • 69