3

I have a xml file with below structure

    <project>
    <dependency id="abc" version="1.2.3.4"/>
    </project>

I need to read this xml and update another xml with the value of id and version. Earlier i did this with an xsl like below, which was working fine:

    <xsl:attribute name="id">
    <xsl:value-of select="@id"/>
    </xsl:attribute>

    <xsl:attribute name="version">
    <xsl:value-of select="@version"/>
    </xsl:attribute>

Now I need to set the attribute value of version as [1.2,1.3), how can I do this? I tried something like this below, but I dont think I'm getting no where.

    <xsl:variable name="MinVersion"/>
    <xsl:variable name="MaxVersion"/>

<xsl:for-each select="tokenize(@version,'.')">

<xsl:if test="(position( )) = 2">
    <xsl:value-of select="concat($MinVersion,.)"/>
</xsl:if>
<xsl:otherwise>
    <xsl:value-of select="concat($MinVersion,.,'.')"/>
</xsl:otherwise>

<xsl:if test="(position( )) = 2">
    <xsl:value-of select="concat($MaxVersion,number(.)+1)"/>
</xsl:if>
<xsl:otherwise>
    <xsl:value-of select="concat($MaxVersion,.,'.')"/>
</xsl:otherwise>
 </xsl:for-each>

How can I do this?

UPDATE: I tried to debug this in Visual Studio and got error message stating tokenize is not a recognized function. After some searching found .NET framework supports only XSLT 1.0 processor. Any solution with 1.0 would be helpful.

2
  • Should your output be like follows: [$MinVersion - Firstnumber.Secondnumber] [$MaxVersion- Firstnumber.Thirdnumber] ? should it always follow this pattern ? Commented Oct 22, 2012 at 14:50
  • Microsoft's XSLT processors in the .NET framework (i.e. XslCompiledTransform and XslTransform) are XSLT 1.0 processors but there are third party XSLT 2.0 processors like the .NET version of Saxon 9 saxon.sourceforge.net/#F9.4HE or AltovaXML (usable in .NET via COM interop) or XmlPrime. So that way you can use the tokenize function in XSLT with .NET. Commented Oct 22, 2012 at 15:53

1 Answer 1

2

Try following, Hope it will solve your problem.(I have Changed my prevous answer, check following new one.)

<xsl:variable name="testVersion" select="@version"></xsl:variable>
<xsl:variable name="separator" select="'.'"></xsl:variable>


<xsl:if test="string-length($testVersion) > 0">

  <xsl:variable name="firstnumber"     select="substring-before($testVersion, $separator)"/>
  <xsl:variable name="after-separator" select="substring-after($testVersion, $separator)"/>       
  <xsl:variable name="secondnumber"    select="substring-before($after-separator, $separator)"/>        

  <xsl:variable name="outputvalue">
    <xsl:text>[</xsl:text>
    <xsl:value-of select="$firstnumber"></xsl:value-of>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="$secondnumber"></xsl:value-of>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$firstnumber"></xsl:value-of>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="number($secondnumber)+1"></xsl:value-of>
    <xsl:text>)</xsl:text>
  </xsl:variable>

  <xsl:value-of select="$outputvalue"/>
</xsl:if>
Sign up to request clarification or add additional context in comments.

4 Comments

Madurika: The output should always be in the following pattern, assuming @version is 1.1.234.5678 I need a value in a variable like below: [firstnumber.secondnumber, firstnumber,secondnumber+1) I'll check the substring option, if the firstnumber is 2 digit number, I guess the proposed solution might fail. Let me check it out though Martin: Saxon is not an feasible option right now, because this will be called inside an MSBuild script.
@KarunakaranPannirSelvam There is no problem calling Saxon from within an MSBuild script. You can use the console version of Saxon.
@Karun please be kind enough to up vote for the answer by clicking up arrow head. .... cheers....!!!!!
I tried to up vote, looks like I need more reputation to do that.

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.