New to xslt and need help renaming entries in xml

Here should go questions about transforming XML with XSLT and FOP.
christian
Posts: 5
Joined: Thu Jun 29, 2006 5:53 pm

New to xslt and need help renaming entries in xml

Post by christian »

Hi

I'm new to all this XSLT and would really appreciate some help transforming the following xml. I'm not even sure if it is possible with xslt.

Here is an example of the source

<?xml version="1.0"?>
<main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/tmp/WordXML/example.xsd">
<form formID="HotelStandardAuditV2" fhUid="963" fhtUid="2645">
<formFieldList>
<formField fieldName="Car688.AtSeatEnt.B1">No</formField>
<formField fieldName="Car688.CCTV.B1">No</formField>
<formField fieldName="Car688.CETnotFull.B1">No</formField>
<formField fieldName="Car688.ExtDoorDisp.B1">No</formField>
<formField fieldName="Car688.ExtDoorDisp.B2">No</formField>
</formFieldList>
</form>
</main>

What I need to do is change the text of the field name for example from "Car688.AtSeatEnt.B1" to "Field1A_SetaInt". I'm happy to hard code the replace using strings rather than trying to make up the replacement text from the source text.

So the result should look something like this

<?xml version="1.0"?>
<main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/tmp/WordXML/example.xsd">
<form formID="HotelStandardAuditV2" fhUid="963" fhtUid="2645">
<formFieldList>
<formField fieldName="Field1A_SetaInt">No</formField>
<formField fieldName="Field2A_SetaInt">No</formField>
<formField fieldName="Field3A_SetaInt">No</formField>
<formField fieldName="Field4A_SetaInt">No</formField>
<formField fieldName="Field5A_SetaInt">No</formField>
</formFieldList>
</form>
</main>

Many thanks for any help you can offer. I've got a couple of books on order and need to read them thououghly I think.
Radu
Posts: 9434
Joined: Fri Jul 09, 2004 5:18 pm

Post by Radu »

Hi Christian,

Maybe the following stylesheet would be useful:

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="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@fieldName">
<xsl:attribute name="fieldName">
<xsl:choose>
<xsl:when test=".='Car688.AtSeatEnt.B1'">Field1A_SetaInt</xsl:when>
<xsl:when test=".='Car688.CCTV.B1'">Field2A_SetaInt</xsl:when>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
The first 2 templates are used to copy the input XML unchanged. The third one is used to modify the value of the "fieldName" attribute and you can add more "xsl:when" statements to replace the values in the XML document with your hardcoded values.

XSLT has some powerful string manipulation functions so you could also consider making up the new values from the old ones if you know a derivation rule that would apply.

Regards, Radu
christian
Posts: 5
Joined: Thu Jun 29, 2006 5:53 pm

Post by christian »

Hi Radu

Thanks so much for your response. This looks great. I'll take a close look at this and the string functions that you mentioned. Still waiting on my books to arrive!

Christian
Post Reply