1
votes

I am trying to produce html output according to the fields in xml file with xslt. And I name them according to grandparent-parent-child-grandchild relation in xml

For instance:

<root>
  <node1>
      <node2>
         <node3>Data</node3>
      </node2>
  </node1>

What i need is creating let's say textbox, with name node1__node2__node3 What i did so far is this

<input type="text" name="node1__
        node2__
        node3__"

But what i want to is:

<input type="text" name="node1__node2__node3__"/>

So it is useless. My xslt to produce this useless output is:

<xsl:template name="chooseNameID">
    <xsl:param name="currentNode"/><!-- in this case currentNode is node3 -->
    <xsl:variable name="fieldNames">
        <xsl:for-each select="$currentNode/ancestor::*">
                <xsl:value-of select="name(.)"/>__
        </xsl:for-each>
    </xsl:variable>

    <xsl:attribute name="name">
        <xsl:value-of select="$fieldNames"/>                            
    </xsl:attribute>

</xsl:template>

I guess the problem is in <xsl:value-of but i cannot find any solution to that.

Thanks

3
Good question, +1. See my answer for a complete, short and easy solution. :)Dimitre Novatchev

3 Answers

1
votes

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node3">
    <xsl:variable name="vName">
     <xsl:for-each select=
      "ancestor-or-self::*[not(position()=last())]">
        <xsl:value-of select="name()"/>
        <xsl:if test="not(position()=last())">__</xsl:if>
     </xsl:for-each>
    </xsl:variable>

    <input type="text" name="{$vName}"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
    <node1>
        <node2>
            <node3>Data</node3>
        </node2>
    </node1>
</root>

produces the wanted, correct result:

<input type="text" name="node1__node2__node3"/>

Do note: The use of AVT (Attribute Value Template) to generate the required output in one short line.

1
votes

The unwanted whitespace, including newlines, is part of a text node literal in the loop.

In the stylesheet document, whitespace-only text nodes are ignored except within xsl:text. However whitespace adjacent to other text is part of that text.

Literal whitespace in the stylesheet can be managed with xsl:text.

    <!-- change this -->
    <xsl:for-each select="$currentNode/ancestor::*">
        <xsl:value-of select="name(.)"/>__
    </xsl:for-each>

    <!-- to this -->
    <xsl:for-each select="$currentNode/ancestor::*">
        <xsl:value-of select="name(.)"/>__<xsl:text/>
    </xsl:for-each>

    <!-- or this -->
    <xsl:for-each select="$currentNode/ancestor::*">
        <xsl:value-of select="name(.)"/>
        <xsl:text>__</xsl:text>
    </xsl:for-each>
0
votes

As normal, after asking a question you find a solution.

Changing <xsl:value-of select="$fieldNames"/> line with this <xsl:value-of select="normalize-space($fieldNames)" worked for me.