fobisthename
fobisthename

Reputation: 165

XML - XSLT - Keeping part of the input XML document

I have this input XML document:

<?xml version="1.0" encoding="UTF-8"?>
<jasperPrint xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<page>
    <firstElement>Some Data</firstElement>
    <secondElement name="2ndElement">
        <subElement id="1SE">
            <firstChild name="1st"/>
            <secondChild>DATA I WANT TO KEEP 1</secondChild>
        </subElement>   
        <subElement id="1SE">
            <firstChild name="1st"/>
            <secondChild>DATA I WANT TO KEEP 2</secondChild>
        </subElement>   
        <subElement id="1SE">
            <firstChild name="1st"/>
            <secondChild>DATA I WANT TO KEEP 3</secondChild>
        </subElement>   
        <subElement id="1SE">
            <firstChild name="1st"/>
            <secondChild>DATA I WANT TO KEEP 4</secondChild>
        </subElement>   
    </secondElement>
</page>
</jasperPrint>

And with XSLT I want to transoform it in order to only keep the <secondChild> elements, like this:

<?xml version="1.0" encoding="UTF-8"?>
<secondChildList>
    <secondChild>DATA I WANT TO KEEP 1</secondChild>
    <secondChild>DATA I WANT TO KEEP 2</secondChild>
    <secondChild>DATA I WANT TO KEEP 3</secondChild>
    <secondChild>DATA I WANT TO KEEP 4</secondChild>
</secondChildList>

This is the XSLT code I'm trying:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>



<xsl:template match="jasperPrint/page/secondElement">

    <secondChildList>

        <xsl:for-each select="subElement">
             <secondChild>
                 <xsl:value-of select="secondChild"/>
            </secondChild>
        </xsl:for-each>

    </secondChildList>

</xsl:template>


</xsl:stylesheet>

Which is just outputing the text in the input XML document, like this:

<?xml version="1.0" encoding="UTF-8"?>Some DataDATA I WANT TO KEEP 1DATA I WANT TO KEEP 2DATA I WANT TO KEEP 3DATA I WANT TO KEEP 4

How can I achieve the output XML document I want with XSLT?

Thank you!

Alexandre Jacinto

Upvotes: 1

Views: 82

Answers (3)

imran
imran

Reputation: 461

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"  xmlns:m="http://jasperreports.sourceforge.net/jasperreports/print"
    exclude-result-prefixes="xs m"
    version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="m:page">
        <secondChildLis>
            <xsl:for-each select="//m:secondChild">
                <secondChild>
                    <xsl:apply-templates/>
                </secondChild>
            </xsl:for-each>

        </secondChildLis>
    </xsl:template>

</xsl:stylesheet>
You may also try it.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163458

The simplest stylesheet to achieve this is probably:

<secondChildList xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:copy-of select="//secondChild"/>
</secondChildList>

Give it a try. Simplified stylesheets, with no xsl:stylesheet wrapper, are very handy for simple jobs like this. But if you want to add something like xsl:output, then you need the full syntax:

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <secondChildList>
      <xsl:copy-of select="//secondChild"/>
    </secondChildList>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Aniket V
Aniket V

Reputation: 3247

You need to change the for-each to

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

or better use

<xsl:apply-templates select="subElement/secondChild"> 

instead of for-each.

Upvotes: 0

Related Questions