Identity transform, remove one filtered element, not working
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 7
- Joined: Sat Aug 18, 2012 2:12 am
Identity transform, remove one filtered element, not working
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:
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:
Any help deeply appreciated!
Bingley
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 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
-
- Posts: 42
- Joined: Sun Jul 15, 2012 12:26 pm
- Location: London
Re: Identity transform, remove one filtered element, not working
Hi.
Using saxon 6.5.5 and correctly formatted your XML what you've submitted works for me.
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>
-
- Posts: 7
- Joined: Sat Aug 18, 2012 2:12 am
Re: Identity transform, remove one filtered element, not working
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:
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:
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.
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>
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>
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service