Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting variables

Status
Not open for further replies.

jd323

Technical User
Sep 1, 2005
7
0
0
GB
My XSL tranform takes the data below, and generates xml output, which is effectively marked-up 'text'. The text-wrapping mark-up in the output is abbreviated to <aaa>. For simplicity within my transform, I have defined these marked-up data as variables.

I effectively want simulate the concat( , ) function (which works only for xs:string) but preserve the mark-up as well. My output should look something like:

$open $data1 $comma $data2 $comma $data3 $close; i.e.

Code:
OUTPUT STRUCTURE
	<aaa>(</aaa>
	<aaa>1.11</aaa>
	<aaa>g</aaa>
	<aaa>, </aaa>
	<aaa>2.22</aaa>
	<aaa>mol</aaa>
        ..... etc.

HOWEVER, the difficulty is that I cannot guarentee that data1, data2 or data3 exists. Thus I need to test each 'piece' of data for existance and whether or not it is first or last (these come with parantheses).

Something along the lines of (in pseudo-code):

CHOOSE
WHEN(first and last)
$open $dataX $close
WHEN(first)
$open $dataX
WHEN(last)
$dataX $close
OTHERWISE
$dataX $comma
/CHOOSE

For a similar solution, dealing with only xs:string please see this post
Code:
INPUT STRUCTURE
<data>
	<data1 unit="g">1.11</data1>
	<data2 unit="mol">2.22</data2>
	<data3 unit="percent">3.33</data3>
</data>

Code:
DECLARED VARIABLES IN TEMPLATE
$data1 = "<aaa>1.11</aaa><aaa>g</aaa>
$data2 = "<aaa>2.22</aaa><aaa>mol</aaa>
$data3 = "<aaa>3.33</aaa><aaa> %</aaa>
$open  = "<aaa>(</aaa>"
$close = "<aaa>, </aaa>"
$comma = "<aaa>)</aaa>"
 
Recursion is probably the best way to accomplish this:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" version="1.0" indent="yes"/>
  <xsl:template match="/">
    <myData>
      <aaa>(</aaa>
      <xsl:call-template name="formatData">
        <xsl:with-param name="nodes" select="data/*"/>
      </xsl:call-template>
      <aaa>)</aaa>
    </myData>
  </xsl:template>
  <xsl:template name="formatData">
    <xsl:param name="nodes"/>
    <xsl:apply-templates select="$nodes[1]"/>
    <xsl:choose>
      <xsl:when test="count($nodes) = 1"/>
      <xsl:otherwise>
        <aaa>,</aaa>
        <xsl:call-template name="formatData">
          <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="node()">
    <aaa>
      <xsl:value-of select="."/>
    </aaa>
    <aaa>
      <xsl:value-of select="@unit"/>
    </aaa>
  </xsl:template>
</xsl:stylesheet>
This doesn't include the grouping though.

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top