Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AlexR 39 posts 61 karma points
    May 25, 2010 @ 12:54
    AlexR
    0

    How to convert string to node set ?

    What is wrong with next code?

    <xsl:variable name="items"
    select="msxml:node-set('&lt;items>&lt;item1>1&lt;/item1>&lt;/items>')" />
    <xsl:value-of select="count($items/items)" />

    It is return 0!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 25, 2010 @ 13:46
    Chriztian Steinmeier
    0

    Hi AlexR,

    Sadly, that's not how the node-set() function works. You'll need an XSLT extension for that...

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 25, 2010 @ 13:50
    Chriztian Steinmeier
    0

    - Refer to this comment to see how the node-set() function can be used: http://our.umbraco.org/forum/developers/xslt/9360-xsl-if-array?p=0#comment34272

    /Chriztian

  • AlexR 39 posts 61 karma points
    May 25, 2010 @ 16:46
    AlexR
    0

    What function can be used to convert string to node set?

    I see that xslt very, very, very bad language.

    I asked about 5 simple questions and got only one answer. All other questions don't have answers.

  • dandrayne 1138 posts 2262 karma points
    May 25, 2010 @ 17:08
    dandrayne
    4

    Hi Alez

    xslt can be frustrating (unless you're as fluent as Chriztian!) but this should work for you

    <xsl:variable name="items">
    <items>
    <item>1</item>
    <item>2</item>
    <item>bucklemyshoe</item>
    </items>
    </xsl:variable>

    <xsl:variable name="itemSet" select="msxml:node-set($items)" />

    <xsl:value-of select="count($itemSet//item)" />
    -- OUTPUTS "3" ---

    <xsl:value-of select="$itemSet//item[position() = 3]" />
    -- OUTPUTS "bucklemyshoe" --

    Hope this helps,
    Dan

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    May 25, 2010 @ 17:26
    Lee Kelleher
    2

    Hi AlexR,

    It's not that XSLT is a bad language, (quite the opposite), it more that there is a misunderstanding that it's a declarative language, but its all too often treated (and abused) as a functional/procedural language.

    XSLT's power is in transforming XML data-sources, (sounds obvious I know), but when trying to create a string-based XML node-set, I can see how this is frustrating and confusing!  Chriztian and Dan's suggestion of creating the XML nodes in a variable, then applying the msxml:node-set function is the best way of achieving this.

    I'm curious what you meant about your "5 simple questions" ... what else is troubling you with XSLT? (we're happy to help)

    Cheers, Lee.

  • AlexR 39 posts 61 karma points
    May 26, 2010 @ 05:17
    AlexR
    0

    I known that code from dandrayne will work, but I have code that ca not be transfered to code like dandrayne's code.

    For example I writes xslt template that generate range sequence of integers.

    <xsl:template name="rangeListLoop">
      <xsl:param name="start" />
      <xsl:param name="count" />
      <xsl:param name="step" />
      <xsl:param name="itemElement" />

      <xsl:if test="$count > 0">
        <xsl:text>&lt;</xsl:text><xsl:value-of select="$itemElement" /><xsl:text>></xsl:text>
          <xsl:value-of select="$start" />
        <xsl:text>&lt;/</xsl:text><xsl:value-of select="$itemElement" /><xsl:text>></xsl:text>

        <xsl:call-template name="rangeListLoop">
          <xsl:with-param name="start" select="$start + $step" />
          <xsl:with-param name="count" select="$count - 1" />
          <xsl:with-param name="step" select="$step" />
          <xsl:with-param name="itemElement" select="$itemElement" />
        </xsl:call-template>
      </xsl:if>

    </xsl:template>

     

    And in this code I used itemElement param to generate integer in different elements, like this:

    <item>1<item><item>2<item>  or  <month>1<month><month>2<month> and etc.

    But I can not use <element name="$itemElement"> because XSLT forbidden use variables in names. But why? What is wrong with generation not static XML elements.

    XSLT created to generate XML and it can not create flexible XML, only static. Why?

     

    I tried to use trick to generate elements as text

    <xsl:text>&lt;</xsl:text><xsl:value-of select="$itemElement" /><xsl:text>></xsl:text>

    But I can not use it also. Because XSLT can not convert string to node set. Why it can not convert string to node set? What wrong with this?

    I so on, so no, so on...

    XSLT bad language. It  is not flexible and have many unusual restrictions.

    Other big restriction that I can not use variables in xpath expressions. I can not generate xpath as string.

    How I can flexible transform XML when XSLT is not flexible, it is static.

    You should hard-code all in your code - it is stupid.

     

    So I think that is better to call C# functions that will use LINQ to XML and can generate anything without unusual restrictions.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    May 26, 2010 @ 09:25
    Lee Kelleher
    1

    Hi AlexR, I do understand your frustrations.  All you want is possible, but there is a learning curve.

    You can use a variable with <xsl:element>, like this:

     <xsl:element name="{$itemElement}"></xsl:element>

    The variable needed to be wrapped in curly brackets.

    The XPath as a string has been a constant cause of confusion/frustration to devs on the forum, mostly due to the encoding of quotes (both single and double).  If you have an example of what you want to do, I'm sure we can help.  I wont promise that it will be pretty code, but it should be achievable!

    Take a look at some of the other forum topics on this:

    http://our.umbraco.org/forum/developers/xslt/8496-Dynamic-XPath-query

    http://our.umbraco.org/forum/developers/xslt/6400-Has-Anyone-Actually-Got-GetXmlNodeByXPath-Working

     

    With all this said, if you are happier developing using the .NET APIs, (C#/VB.NET, LINQ, etc), then I'd say go for it! That's the beauty of Umbraco, its flexible to develop with whatever you are most comfortable with, whether that's XSLT, C#, Python, (and soon more). :-)

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft