1

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, '&#x9;')"/>
    <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

1 Answer 1

2

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)}">.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Can't believe it could've been so simple. Thank you!
@helderdarocha Yes I had to wait 4 minutes to mark it as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.