XSLT 2 Reverse tokenized result

I am a beginner attempting to work with XSLT 2.0 to move an XML file from one schema to another schema. I’ve had a lot of success with most of the document but one piece is holding me up. The input XML element is geographic information, for example:

<spatial>United States -- Washington -- Asotin county; United States -- Idaho;</spatial> 

In the above example, we have two locations, going from macro to micro, separated by a semicolon (;). However, the resultant schema wants them in reverse order and each in it’s own tag, and separated by commas, i.e.:

<mods:geographic>Asotin county, Washington, United States</mods:geographic> <mods:geographic>Idaho,  United States</mods:geographic> 

We are currently using the following, which works but limits us to 6 possible options (fine for our test dataset but we have a large number of datasets to convert and no direct control over them):

<xsl:template match="spatial">     <xsl:for-each select="tokenize(., ';')">         <xsl:variable name="spatialPart" select="tokenize(., ' -- ')"/>             <xsl:if test="normalize-space(.) != ''">                 <!--spatialPart refers to each segment of a full place name; tokenize/separate each segment at space dash dash space-->                             <geographic>                     <xsl:value-of separator=", "                         select="$spatialPart[6], $spatialPart[5], $spatialPart[4], $spatialPart[3], $spatialPart[2], $spatialPart[1]"                     />                 </geographic>             </xsl:if>     </xsl:for-each> </xsl:template> 

I’m hoping to come up with something that can take any number of arguments. Based on other posts, I’ve tried working a numeric descending sort into this but it throws an error each time (I believe because it wants the sort to be the first instruction but I don’t know how to reference it later if it is).

Any assistance is greatly appreciated.

Add Comment
1 Answer(s)

How about:

<xsl:template match="spatial">     <xsl:for-each select="tokenize(., ';')[normalize-space()]">         <geographic>             <xsl:value-of select="reverse(tokenize(., ' -- '))" separator=", "/>         </geographic>     </xsl:for-each> </xsl:template> 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.