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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT function and VS2005

Status
Not open for further replies.

GuardianOfTheFlame

Programmer
Sep 4, 2007
31
0
0
IT
Hi all,
I use Visual Studio 2005 to create my xsl transformations but I don't understand how can I use functions.
Searching on the internet I see that exists the tag <xsl:function> but VS doesn't recognize it (and in the MSDN index this tag is not documented)... is there an other way to create functions?

I would like to make a function called IsText that return a boolean, so I mustn't repeat this code every time I want to use it:
Code:
test="@datatype='3' or @datatype='4' or @datatype='5' or @datatype='6' or @datatype='7' or @datatype='8' or @datatype='9'"

I cannot use a template 'cause I cannot call it in a <xsl:if> tag, right? so I think I must create a function to do this...

Thanks,
Salo
 
<xsl:function> is an XSLT 2.0 element. It allows you to write custom xpath functions that are called from a special namespace. Microsoft are working on XSLT 2.0 support, but it will not ship even with .net 3.5.

In the mean time, you can use saxon with .net for XSLT 2.0 support -
Or, in your case, it will be easier to do it using XSL 1.0 in a different way. Off the top of my head, you could create a template that returns a boolean and call the template in a variable before the <xsl:if>. Maybe you could outline the problem in a bit more detail.

Jon

"I don't regret this, but I both rue and lament it.
 
to create a template that returns a boolean, must I use the keyword "as" in <xsl:template>? If so, VS2005 doesn't recognize it... but in XSLT 1.0 what can you do if you need a function?
I have an XSLT that transforms an XML in a CREATE TABLE. This is an example of input XML:
Code:
<xml>
	<form name="my_Form" pages="1" w="600" h="800" format="A4" style="nubistyle_1.css">
		<page domid="container" name="container" orientation="portrait">
			<items>
				<item type="text" x="30" y="100" w="100" h="20" name="text_1" domid="text_1" tooltip="" group="" tabindex="" datatype="3" minlen="" maxlen="" bordercolor="" bgcolor="" forecolor="" unique="0" required="0" disabled="0" multiline="0">
					<content><![CDATA[Default Value]]></content>

				</item>
			</items>
		</page>
	</form>
</xml>

datatype is an integer value that specify the type of the input of a textbox that the user define. It can have this values:
Not defined = 0
Integer = 1
Float = 2
String = 3
String:URI = 4
String:Letters =5
... and so on
The XSLT transforms this input in a CREATE TABLE (for MySQL) and I often test if the item type is a text (the various subtypes of string at this moment are always treat as string) so I would like to create a function to avoid to repeat the code.
For example I use it in this fragment of XSL:
Code:
  <!--Define the column for a text item-->
  <xsl:template name="column_from_text">
    <xsl:value-of select="@domid"/>
    <xsl:call-template name="get_datatype">
      <xsl:with-param name="type_id">
        <xsl:value-of select="@datatype" />
      </xsl:with-param>
      <xsl:with-param name="length">
        <xsl:value-of select="@maxlen" />
      </xsl:with-param>
    </xsl:call-template>
    <xsl:if test="@required='1'">
      <xsl:text> NOT NULL</xsl:text>
    </xsl:if>
    <xsl:if test="./content!=''">
      <xsl:choose>
        <xsl:when test="@datatype='3' or @datatype='4' or @datatype='5' or @datatype='6' or @datatype='7' or @datatype='8' or @datatype='9' or @datatype='10'">
          <xsl:if test="not(@maxlen > 1000)">
            <xsl:text> DEFAULT '</xsl:text>
            <xsl:value-of select="./content"/>
            <xsl:text>'</xsl:text>
          </xsl:if>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text> DEFAULT </xsl:text>
          <xsl:value-of select="./content"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
 
Why not do:
Code:
<xsl:when test="@datatype > 2 and @datatype < 11">

Jon

"I don't regret this, but I both rue and lament it.
 
you're right, in this way is more compact, however it's possible that other datatypes will be added so I prefer to create a function for a simpler maintenance of the code.
Moreover I would like to understand how can I create functions to use them in other (and more complex) parts of the transformation.
I relate this trivial portion of code 'cause I want to understand the method how can I face similar problems, I don't want to ask you to solve them for me ;-)
Thanks again!
Salo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top