conditionally rename tags upon data/content
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 2
- Joined: Sat Feb 02, 2008 12:59 am
conditionally rename tags upon data/content
hi,
i cannot figure out how write a XSLT to rename only specific tags and output as XML.
i received xml with "duplicate" xml-tags.
meaning: sometimes the xml-tag <field> should be <field1> and sometime <field2>.
this depends on the xml-tag <field_type> which is put just before <field>.
<field_type>A</fiel_type>
<field>textisenteredhere</field>
<field_type>B</field_type>
<field)textiseneterdherealso, but should have been labeled different</field>
So, if the content of <field_type> = "A" the following <field> should be renamed <field1>. if the content of <field_type> = "B" the following <field> should be renamed <field2>.
The XMl my client has gvenme contains close to 6000 lines, so i would prefer a XSLT to transform this and copy nodes with renamed tags.
thanks for any help
i cannot figure out how write a XSLT to rename only specific tags and output as XML.
i received xml with "duplicate" xml-tags.
meaning: sometimes the xml-tag <field> should be <field1> and sometime <field2>.
this depends on the xml-tag <field_type> which is put just before <field>.
<field_type>A</fiel_type>
<field>textisenteredhere</field>
<field_type>B</field_type>
<field)textiseneterdherealso, but should have been labeled different</field>
So, if the content of <field_type> = "A" the following <field> should be renamed <field1>. if the content of <field_type> = "B" the following <field> should be renamed <field2>.
The XMl my client has gvenme contains close to 6000 lines, so i would prefer a XSLT to transform this and copy nodes with renamed tags.
thanks for any help
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: conditionally rename tags upon data/content
Hi,
Start with a recursive copy template and then add a match for each case that you want to catch and output field1, field2, etc.
Best Regards,
George
Start with a recursive copy template and then add a match for each case that you want to catch and output field1, field2, etc.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="field[preceding-sibling::*[1][self::field_type and .='A']]">
<field1>
<xsl:apply-templates select="node() | @*"/>
</field1>
</xsl:template>
<xsl:template match="field[preceding-sibling::*[1][self::field_type and .='B']]">
<field2>
<xsl:apply-templates select="node() | @*"/>
</field2>
</xsl:template>
</xsl:stylesheet>
George
George Cristian Bina
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