Reputation: 81
I need to parse a tab-delimited string into an xml node structure defined in a variable. I used tokenize() to get the string into an "array" variable and have a variable that tells me what tag I should give to each element in the array based on its position. I do a for-each on the array and assign each element in it to an xml tag to produce the output.
Here's the input:
<Data>Jane Doe /t Atlantis /t 4-1-1999 /t [email protected]<Data>
Here's the XSLT:
<xsl:variable name="MyXMLStructure">
<Field Position="1">Name</Field>
<Field Position="2">Location</Field>
<Field Position="3">DateOfBirth</Field>
<Field Position="4">Email</Field>
</xsl:variable>
<xsl:template name="processData">
<xsl:param name="pText"/>
<xsl:variable name="DataFields" select="tokenize($pText, '	')"/>
<xsl:element name="PersonData">
<xsl:for-each select="$DataFields">
<xsl:element name="{$MyXMLStructure/Field[@Position = position()]}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="processData">
<xsl:with-param name="pText" select="/Data"/>
</xsl:call-template>
</xsl:template>
Here's the expected output:
<PersonData>
<Name>Jane Doe</Name>
<Location>Atlantis</Location>
<DateOfBirth>4-1-1999</DateOfBirth>
<Email>[email protected]</Email>
</PersonData>
I get this error:
Invalid Element Name. Invalid QName {Name Location ...}
I'm guessing that the position() of the array isn't working well and the name of the element isn't being assigned properly.
Please help!
EDIT: Edited XSLT code
Upvotes: 1
Views: 729
Reputation: 167716
I think you want
<xsl:for-each select="$DataFields">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="{$MyXMLStructure/Field[@Position = $pos]}">
<xsl:value-of select="normalize-space()"/>
</xsl:element>
</xsl:for-each>
For a more efficient access you could define a key <xsl:key name="k1" match="Field" use="xs:integer(@Position)"/>
and then use <xsl:element name="{key('k1', position(), $MyXMLStructure)}">
.
Upvotes: 2