0

I have a text file as given below:

value1 value2 value3 value4

I want to convert it as following xml using XSLT

 <values>
    <value>value1</value>
    <value>value2</value>
    <value>value3</value>
     <value>value4</value>
   </values>

Thanks in advance.

3
  • XSLT doesn't entertain Text to XML conversion. You can do the other way though(XML to Text). Commented Mar 27, 2014 at 6:20
  • 1
    @Linhamurthy CS: The guy is using Java, so why are you assuming s/he's still on XSLT 1.0? OK, when people don't say which version, that often means they aren't aware of the difference, but since they need XSLT 2.0 to solve the problem, and it's available in their environment, your response saying "can't be done" is unhelpful and incorrect. Commented Mar 27, 2014 at 7:44
  • @MichaelKay : Thanks, Dr.Kay.. That was my ignorance. I wasn't aware of this capability of XSLT2.0. Commented Oct 25, 2014 at 0:42

2 Answers 2

1

Assuming XSLT 2.0,

<xsl:template name="main">
 <values>
  <xsl:for-each select="tokenize(unparsed-text('input.txt'), '\s+')">
   <value><xsl:value-of select="."/></value>
  </xsl:for-each>
 </values>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

0

if you can edit your input such that it contains a root element, and a tab character as a separator, such as below:

<root>value1    value2  value3  value4</root>

then, you can apply the following stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <values>
            <xsl:call-template name="tokenizeString">
                <xsl:with-param name="list" select="."/>
                <xsl:with-param name="delimiter" select="'&#x9;'"/>
            </xsl:call-template>
        </values>
    </xsl:template>

    <xsl:template name="tokenizeString">
        <!--passed template parameter -->
        <xsl:param name="list"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="contains($list, $delimiter)">
                <value>
                    <!-- get everything in front of the first delimiter -->
                    <xsl:value-of select="substring-before($list,$delimiter)"/>
                </value>
                <xsl:call-template name="tokenizeString">
                    <!-- store anything left in another variable -->
                    <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$list = ''">
                        <xsl:text/>
                    </xsl:when>
                    <xsl:otherwise>
                        <value>
                            <xsl:value-of select="$list"/>
                        </value>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

and produce:

<values>
   <value>value1</value>
   <value>value2</value>
   <value>value3</value>
   <value>value4</value>
</values>

3 Comments

This is an XSLT 1.0 "solution" that doesn't solve the question as given. See my response for a much more concise and complete XSLT 2.0 solution.
@Joel M. Lamsen: Suppose I want to add different node name theses values? How can I do that.
please post this as another question as this may require a different solution

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.