0

This may sound repetitive question but I am not able to understand some basic xslt concepts

Actually I am very new to the concepts of xslt and have been working on how to transform a text document to xml .

However I found out following XSLt in stackoverlow(Regular text file to XML using XSLT)

     <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 xmlns:my="my:my" exclude-result-prefixes="ext my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <my:fieldNames>
  <name>FirstName</name>
  <name>LastName</name>
  <name>City</name>
  <name>State</name>
  <name>Zip</name>
 </my:fieldNames>

 <xsl:variable name="vfieldNames" select=
  "document('')/*/my:fieldNames"/>

 <xsl:template match="/">
  <xsl:variable name="vrtfTokens">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:variable name="vTokens" select=
       "ext:node-set($vrtfTokens)"/>

  <results>
   <xsl:apply-templates select="$vTokens/*"/>
  </results>
 </xsl:template>

 <xsl:template match="text()" name="tokenize">
  <xsl:param name="pText" select="."/>

     <xsl:if test="string-length($pText)">
       <xsl:variable name="vWord" select=
       "substring-before(concat($pText, '^'),'^')"/>

       <word>
        <xsl:value-of select="$vWord"/>
       </word>

       <xsl:call-template name="tokenize">
        <xsl:with-param name="pText" select=
         "substring-after($pText,'^')"/>
       </xsl:call-template>
     </xsl:if>
 </xsl:template>

 <xsl:template match="word">
  <xsl:variable name="vPos" select="position()"/>

  <field>
      <xsl:element name="{$vfieldNames/*[position()=$vPos]}">
      </xsl:element>
      <value><xsl:value-of select="."/></value>
  </field>
 </xsl:template>
</xsl:stylesheet>

but actually could not get what is happening in this xslt like what doe the following code mean

<xsl:variable name="vfieldNames" select="document('')/*/my:fieldNames"/>  (What is selected in the following code)?

Folloing is text document

XXX^YYYY^AAAAA^XXXXXX^AAAAAA....

Following is the output

  <name>XXX</name>
<l_name>YYYY</l_name>

Thanks

Community
  • 1
  • 1
Uselesssss
  • 2,127
  • 6
  • 28
  • 37
  • That's a _lot_ of concepts (variables, params, templates, matches, apply-templates, call-template, string functions etc.) packed into one stylesheet which makes your question a very broad one. Find yourself a tutorial on XSLT and study these concepts _in isolation_. – Mathias Müller Feb 14 '14 at 12:59
  • I am unable to understand this line – Uselesssss Feb 14 '14 at 13:02
  • Please edit your question to make it clear that you'd like to have an explanation for exactly this line of code. Thanks! – Mathias Müller Feb 14 '14 at 13:04

2 Answers2

1
  1. I don't think this can work with your document. Despite what the other thread's title says, the input they are using is a valid XML document. The input you are showing is just a text file, and AFAIK there is no way to process it using XSLT 1.0.

  2. To answer your question: the expression:

    document('')/*/my:fieldNames

selects the following element in the stylesheet itself:

 <my:fieldNames>
  <name>FirstName</name>
  <name>LastName</name>
  <name>City</name>
  <name>State</name>
  <name>Zip</name>
 </my:fieldNames>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
1

In this particular line:

<xsl:variable name="vfieldNames" select="document('')/*/my:fieldNames"/>

document('') selects data from the stylesheet it is executed in (that is, it reads the XML document whose base URI corresponds to the URI of the XSLT instruction in question, see Michael Kay's answer here).

So, the following is retrieved and stored as the content of the variable $vfieldNames:

<my:fieldNames>
  <name>FirstName</name>
  <name>LastName</name>
  <name>City</name>
  <name>State</name>
  <name>Zip</name>
</my:fieldNames>
Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75