Page 1 of 1

Remove group levels

Posted: Wed Mar 14, 2012 5:21 pm
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>

Re: Remove group levels

Posted: Thu Mar 15, 2012 1:16 pm
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

Re: Remove group levels

Posted: Fri Mar 16, 2012 2:47 pm
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

Re: Remove group levels

Posted: Fri Mar 16, 2012 3:36 pm
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