So, I’m trying to get some XML into an easy format for dumping into a DB. This is what it looks like:
<?xml version="1.0" encoding="UTF-8"?>
<detail type="Courses Taken" gm_recid="FNBYHVW(,7()E)S" >
<properties>
<property name="reference" >
<property_string>M2</property_string>
</property>
<property name="Month YYYY" db_name="TITLE" >
<property_string></property_string>
</property>
<property name="City, State" db_name="LINKACCT" >
<property_string></property_string>
</property>
</properties>
</detail>
which I’d prefer looked like:
<?xml version="1.0" encoding="UTF-8"?>
<courses_taken>
<gm_recid>FNBYHVW(,7()E)S</gm_recid>
<reference>M2</reference>
<date></date>
<location></location>
</courses_taken>
I’m not too familiar with XSL, so I’m at a loss for where to start. Can someone point me in the right direction?
This complete XSLT transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation: Using a variation of the double-translate method first proposed by @Michael Kay:
If we have a string
$sand a string of only allowable characters$validand we want to get from$sanother string$s2which contains only the valid character of$s(in their original order), this can be achieved by evaluating the following XPath expression:In this particular case we want not to delete the invalid characters, but to replace each of them with
"_".We assume that a candidate name will not exceed the length of 30 and use a fixed-length replacement string (this of course can be made as large as wanted):