0

I am getting an input structure as something like this

 <ParameterSet>2|InterfaceMethod|EQ|I|GenericQuery|NIL</ParameterSet>
 <ParameterSet>1|TargetFilename|EQ|I|VendorMaster|NIL</ParameterSet>

the output should look something like below

  <Parameter>
    <Expression>2</Expression>
    <Parametername>InterfaceMethod</Parametername>
    <Parameter_Opt>EQ</Parameter_Opt>
    <Parameter_Sign>I</Parameter_Sign>
    <Range_Low_Value>GenericQuery</Range_Low_Value>
    <Range_High_Value>NIL</Range_High_Value>
  </Parameter>
  <Parameter>
    <Expression>1</Expression>
    <Parametername>TargetFilename</Parametername>
    <Parameter_Opt>EQ</Parameter_Opt>
    <Parameter_Sign>I</Parameter_Sign>
    <Range_Low_Value>VendorMaster</Range_Low_Value>
    <Range_High_Value>NIL</Range_High_Value>
  </Parameter>

My Problem is the tag gets converted to <ParameterSet&gt and am not able to use foreach when I write into another repetitive structure.

Can anyone provide some sample code.

4 Answers 4

3

The operation you are looking for - turning lexical XML into a tree of nodes - is called parsing. Some XSLT processors have an extension function, e.g. saxon:parse(), that does this (in XSLT 3.0 it's available out-of-the-box as fn:parse-xml()). With other processors, you may be able to write your own extension function by calling out to Java or Javascript.

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

Comments

0

Your input data isn't XML, possibly it's encoded (escaped) XML. So, you need to turn it into well-formed XML, in other words perform pre-processing. Then apply XSL transform.

1 Comment

Yes, and I think the question was, "Can I do this preprocessing also in XSLT?" The answer seems to be "Yes" for XSLT 3.0, and "Perhaps, using a processor-specific extension" for earlier versions.
0

xslt is good at rearranging xml into different xml, not at changing non-xml into xml. While you could do this with xslt using lots of nested substring-before or the like, it will be much much easier if you can run it through sed or something first to create xml out of the |-delimited string.

Comments

0

Thanks everyone for your inputs, I wanted this solution in XSLT as that is only processor I had and also it is running on version 1.0. Got it working with the code below

        <xsl:variable name="TempArea">
&lt;ParameterSet&gt;2|InterfaceMethod|EQ|I|GenericQuery|NIL&lt;/ParameterSet&gt;
&lt;ParameterSet&gt;1|TargetFilename|EQ|I|VendorMaster|NIL&lt;/ParameterSet&gt;
&lt;ParameterSet&gt;1|CompanyCode|EQ|I|4900|NIL&lt;/ParameterSet&gt;</xsl:when>
        </xsl:variable>


            <xsl:call-template name="for.loop.Parameters">
                <xsl:with-param name="sourceNodes" select="substring-after($TempArea,'&lt;ParameterSet&gt;')"/>
            </xsl:call-template>

    <xsl:template name="for.loop.Parameters">
        <xsl:param name="sourceNodes"/>
        <xsl:variable name="temp">
            <xsl:choose>
                <xsl:when test="string-length($sourceNodes) &gt; '0'">
                    <xsl:value-of select="substring-before($sourceNodes,'&lt;/ParameterSet&gt;')"/>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="Expression" select="substring-before($temp, '|')"/>
        <xsl:variable name="remaining" select="substring-after($temp, '|')"/>
        <xsl:variable name="Name" select="substring-before($remaining, '|')"/>
        <xsl:variable name="remainingNext" select="substring-after($remaining, '|')"/>
        <xsl:variable name="Option" select="substring-before($remainingNext, '|')"/>
        <xsl:variable name="remainingNext1" select="substring-after($remainingNext, '|')"/>
        <xsl:variable name="Sign" select="substring-before($remainingNext1, '|')"/>
        <xsl:variable name="remainingNext2" select="substring-after($remainingNext1, '|')"/>
        <xsl:variable name="LowValue" select="substring-before($remainingNext2, '|')"/>
        <xsl:variable name="HighValue" select="substring-after($remainingNext2, '|')"/>

        <Parameter>
            <Expression>
                <xsl:value-of select="$Expression"/>
            </Expression>
            <Parametername>
                <xsl:value-of select="$Name"/>
            </Parametername>
            <Parameter_Opt>
                <xsl:value-of select="$Option"/>
            </Parameter_Opt>
            <Parameter_Sign>
                <xsl:value-of select="$Sign"/>
            </Parameter_Sign>
            <Range_Low_Value>
                <xsl:value-of select="$LowValue"/>
            </Range_Low_Value>
            <Range_High_Value>
                <xsl:value-of select="$HighValue"/>
            </Range_High_Value>
        </Parameter>

        <xsl:variable name="test">
            <xsl:value-of select="substring-after($sourceNodes,'&lt;/ParameterSet&gt;')"/>
        </xsl:variable>
        <xsl:if test="string-length($test)&gt; 1 ">
            <xsl:call-template name="for.loop.Parameters">
                <xsl:with-param name="sourceNodes">
                    <xsl:value-of select="substring-after($test,'&lt;ParameterSet&gt;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

Comments

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.