coder006
coder006

Reputation: 525

How to fetch contents of value attribute and split them in XSLT if they are a list of strings?

I have an xml data node:

<data field="field_value" value="["value1", "value2"]">
Evacuate - Relocate as instructed in the instruction, Prepare - Make preparations per the                  instruction
</data>

I want to get the contents of value attribute(value1 and value2) and split them into nodes like this:

<fieldValue>value1</fieldValue>
<fieldValue>value2</fieldValue>

Right now i am trying to do it like this :

<xsl:if test="data[@field='field_value']">
    <xsl:call-template name="split-into-nodes">
        <xsl:with-param name="string"><xsl:value-of  select="data[@field='field_value']/@value"/>
        </xsl:with-param>
        <xsl:with-param name="node-name">fieldValue</xsl:with-param>
    </xsl:call-template>
</xsl:if>

But the result that i am getting is a bit weird :

<fieldValue>["value1"</fieldValue>
<fieldValue>"value2"]</fieldValue>

Please help...thanks in advance!!!

Upvotes: 3

Views: 131

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

Your source is not well-formed XML, and will not be parsed.

Probably the actual contents of your file are:

<data field="field_value" value="[&quot;value1&quot;, &quot;value2&quot;]">
    Evacuate - Relocate as instructed in the instruction, Prepare - Make preparations per the instruction
</data>

Which is parseable and has the same appearance when printed.

If that is the case, you can use translate() to strip the &quot; character and the square brackets [ and ], by changing your code to this:

<xsl:with-param name="string">
    <xsl:value-of  select="translate(data[@field='field_value']/@value, '&quot;][', '')"/>
</xsl:with-param>

Upvotes: 2

Related Questions