Remove group levels

Here should go questions about transforming XML with XSLT and FOP.

Remove group levels

Postby Marzipan » Wed Mar 14, 2012 5:21 pm

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>
Marzipan
 
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Re: Remove group levels

Postby george » Thu Mar 15, 2012 1:16 pm

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
george
Site Admin
 
Posts: 2061
Joined: Thu Jan 09, 2003 2:58 pm

Re: Remove group levels

Postby Marzipan » Fri Mar 16, 2012 2:47 pm

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
Marzipan
 
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Re: Remove group levels

Postby adrian » Fri Mar 16, 2012 3:36 pm

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
adrian
 
Posts: 1332
Joined: Tue May 17, 2005 4:01 pm


Return to XSLT and FOP

Who is online

Users browsing this forum: No registered users and 0 guests