简体   繁体   中英

how to get required tag value from an xml file in .xsl file?

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.

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).

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM