Remove group levels

Here should go questions about transforming XML with XSLT and FOP.
Marzipan
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Remove group levels

Post by Marzipan »

I have XML that contains elements that contain other elements but not values, they all have 'Group' in the element name. Is it possible to remove the tags that contain 'Group'; found the function syntax:
not(contains(name(),'Group') - i'm not sure where it would go?

Code: Select all

<my:BestPracticeGroup>
<my:BPRepeatSection>
<my:BPDetails>Complete central line insertion checklist in real time</my:BPDetails>
<my:ActionsGroup>
<my:ActionsTable>
my:DateImplementBP>2012-02-02</my:DateImplementBP>
<my:DateCompleteBP>2012-03-02</my:DateCompleteBP>
</my:ActionsTable>
<my:ActionsTable>
</my:ActionsTable>
</my:ActionsGroup>
</my:BPRepeatSection>
</my:BestPracticeGroup>
Here is my current xsl:

Code: Select all

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

<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>

<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>

</xsl:stylesheet>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Remove group levels

Post by george »

You can start with a recursive copy template and just add a rule that matches the elements ending with 'Group' for example and do the specific process you need there. In the example below we just apply templates on their content, thus removing that element but keeping its content:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ends-with(name(), 'Group')]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Regards,
George
George Cristian Bina
Marzipan
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Re: Remove group levels

Post by Marzipan »

Great. I went with contains(). FYI: Got error with ends-with()

Engine name: Saxon6.5.5
Severity: warning
Description: Error in expression *[ends-with(name(), 'Group')]: Unknown system function: ends-with
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Remove group levels

Post by adrian »

Hi,

The stylesheet has the version 2.0, so it won't work with Saxon 6.5.5.
When you try to apply the transformation, Oxygen even gives you a confirmation message and recommends changing the processor to Saxon-HE/PE/EE.

Edit the transformation scenario: Document -> Transformation -> Configure Transformation Scenario, Edit and change the Transformer to Saxon-PE.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply