Identity transform, remove one filtered element, not working

Here should go questions about transforming XML with XSLT and FOP.
bingley
Posts: 7
Joined: Sat Aug 18, 2012 2:12 am

Identity transform, remove one filtered element, not working

Post by bingley »

Hi, all,
I'm a newbie to XSLT and a non-programmer (unfortunately). I'm trying to copy all of a large XML document into a new XML document, except I want to remove one element and its children if one of its child elements contains a specific value.

Source XML:

Code: Select all

<rootElement>
<TitleWork>
...
<roles>
<Name-Role>
<party xl:href="/Name/key/2708 xl:title="Smith, John"></party>
<principalRole>N</principalRole>
<role xl:href="/Code/key/ROLE//INE" l:title="Interviewee">INE</role>
</Name-Role>
<Name-Role>
<party xl:href="/Person/key/279613-1" xl:title="Johnson, Joe"></party>
<principalRole>N</principalRole>
<role xl:href="/Code/key/ROLE//INE" xl:title="Interviewee">INE</role>
</Name-Role>
<Name-Role>
<party xl:href="/Person/key/373022-1" xl:title="McColl, Steven"></party>
<principalRole>N</principalRole>
<role xl:href="/Code/key/ROLE//INR" xl:title="Interviewee">INR</role>
</Name-Role>
</roles>
...
</TitleWork>
<TitleWork> ...
</RootElement>
I need to remove all <Name-Role> elements and children when /role="INR".
I thought two simple templates would do the trick, but the Name-Role elements and children were copied to the result file -- no idea why.

My failed attempt:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xl="http://www.w3.org/TR/xlink">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"></xsl:output>
<xsl:template match="/|@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="//Name-Role[role='INR']"/>
</xsl:stylesheet>

Any help deeply appreciated!

Bingley
William
Posts: 42
Joined: Sun Jul 15, 2012 12:26 pm
Location: London

Re: Identity transform, remove one filtered element, not working

Post by William »

Hi.

Using saxon 6.5.5 and correctly formatted your XML what you've submitted works for me.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<rootElement>
<TitleWork>
<roles>
<Name-Role>
<party href="/Name/key/2708" title="Smith, John"/>
<principalRole>N</principalRole>
<role href="/Code/key/ROLE//INE" title="Interviewee">INE</role>
</Name-Role>
<Name-Role>
<party href="/Person/key/279613-1" title="Johnson, Joe"/>
<principalRole>N</principalRole>
<role href="/Code/key/ROLE//INE" title="Interviewee">INE</role>
</Name-Role>
</roles>
</TitleWork>
</rootElement>
bingley
Posts: 7
Joined: Sat Aug 18, 2012 2:12 am

Re: Identity transform, remove one filtered element, not working

Post by bingley »

Thanks for your quick reply. I was able to remove the one element as described below. The reason I was doing that in the first place was to simplify sorting problem. With the element removed as described below, I thought I could just sort on the @xl:title value of the first role element in each TitleWork grouping. That@xl:title value is usually in the form lastname, firstname, for example:

Code: Select all

    <?xml version="1.0" encoding="UTF-8"?>
<rootElement>
<TitleWork>
<roles>
<Name-Role>
<party href="/Name/key/2708" title="Smith, John"/>
<principalRole>N</principalRole>
<role href="/Code/key/ROLE//INE" title="Interviewee">INE</role>
</Name-Role>
<Name-Role>
<party href="/Person/key/279613-1" title="Johnson, Joe"/>
<principalRole>N</principalRole>
<role href="/Code/key/ROLE//INE" title="Interviewee">INE</role>
</Name-Role>
</roles>
</TitleWork>

[here are many more TitleWork element groups; each TitleWork group should appear in the result document in alphabetical order by last name in first role element]

</rootElement>
Aside from this sort, I just want to copy over the full XML document as XML. Here's my failed stylesheet, copied out of Oxygen:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xl="http://www.w3.org/TR/xlink">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"></xsl:output>
<xsl:strip-space elements="*"></xsl:strip-space>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"></xsl:apply-templates>
<xsl:for-each select="//TitleWork">
<xsl:sort select="descendant::party[1]/@xl:title"></xsl:sort>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The source document is the XML document generated when that one element was removed (per my first question). That XML document does validate. When I try to do the sort, all elements and attributes are copied over to the new document, but the TitleWork groups are not sorted. I've tried at least a hundred ways of doing the sort, no luck. Many, many thanks.
bingley
Posts: 7
Joined: Sat Aug 18, 2012 2:12 am

Re: Identity transform, remove one filtered element, not working

Post by bingley »

I'm sorry, I meant that the sort needs to be done on the xl:title attribute in the party element, not the role element. My brain is so fried. Thank you!
Post Reply