Get Text from Nodeset

Here should go questions about transforming XML with XSLT and FOP.
FJJCENT
Posts: 25
Joined: Sun Apr 22, 2018 10:17 pm

Get Text from Nodeset

Post by FJJCENT »

Hi everybody, I work in xslt 1..0 and I have just get an Array and convert it to a Node Set by the msxsl:node-set() function so i get an array with 12 elements as you can see in the code, but which sentence I need to introduce for getting an text value of for example position 3 of the array I think that i've tested all the possibilities and I always get a Nodeset and not a text value

<xsl:variable name="urlSchema" select="string('http://www.info.es/es/fr/data/2017-09-12/space.xsd')" /> --> Array by split by the / character

<xsl:variable name="arraySchema">
<xsl:call-template name="split">
<xsl:with-param name="str" select="$urlSchema" />
<xsl:with-param name="splitString" select="'/'" />
</xsl:call-template>
</xsl:variable>

<xsl:variable name="cvt" select="msxsl:node-set($arraySchema)" /> --> convert into Array
<xsl:variable name="Totcvt" select="$cvt/node()" /> ---> now it has 12 elements
<xsl:variable name="uno" select="$Totcvt[3]" /> ---- access to third element (nodeset)
<xsl:variable name="dos" select="$uno/text()" /> --> also nodeset
<xsl:variable name="cuenta" select="count($Totcvt)" /> ---> number 12

Which Sentence for getting Text and not Nodeset ???

<xsl:template match="/">
<xsl:copy-of select="$uno"/>
<xsl:copy-of select="$dos"/>
<xsl:value-of select="$cuenta"/>
</xsl:template>


Best Regards
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Get Text from Nodeset

Post by adrian »

Hi,

The 'split' template is missing from your code. We need that to establish what you put in 'arraySchema' in the first place.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
FJJCENT
Posts: 25
Joined: Sun Apr 22, 2018 10:17 pm

Re: Get Text from Nodeset

Post by FJJCENT »

I send it

<xsl:template name="split">
<xsl:param name="str" select="."/>
<xsl:param name="splitString" />
<xsl:choose>
<xsl:when test="contains($str,$splitString)">
<data>
<xsl:value-of select="substring-before($str,$splitString)"/>
</data>
<xsl:call-template name="split">
<xsl:with-param name="str"
select="substring-after($str,$splitString)"/>
<xsl:with-param name="splitString" select="$splitString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<data>
<xsl:value-of select="$str"/>
</data>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Get Text from Nodeset

Post by adrian »

You're splitting by '/' and your split template uses elements named 'data', so you're getting in $arraySchema:

Code: Select all

<data>http:</data>
<data></data>
<data>www.info.es</data>
<data>es</data>
<data>fr</data>
<data>data</data>
<data>2017-09-12</data>
<data>space.xsd</data>
Not sure why yours had 12. I see only 8 on this example ('http://www.info.es/es/fr/data/2017-09-12/space.xsd').
Anyway, if you just want to get to the text use:

Code: Select all

<xsl:value-of select="$cvt/data[8]/text()"/>
Where data[8] is the eighth ('space.xsd').

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
FJJCENT
Posts: 25
Joined: Sun Apr 22, 2018 10:17 pm

Re: Get Text from Nodeset

Post by FJJCENT »

Thank Adrian, it works an solve my problem, but it's very strange because if I check directly the expresion $cvt/data[$cuenta]/text() it gives the correct data but if Icheck the contains of the variable $uno again yields a nodeset.

but for me it's enough

<xsl:variable name="cuenta" select="count($Totcvt)" />
<xsl:variable name="uno" select="$cvt/data[$cuenta]/text()"/>
$uno --> NodeSet (dimension(1))

Again Thanks
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Get Text from Nodeset

Post by adrian »

Hi,

Convert it to string, if that's what you need:

Code: Select all

<xsl:variable name="uno" select="string($cvt/data[$cuenta]/text())"/>
Note that you'll have to use <xsl:value-of select="$uno"/> after this (instead of xsl:copy-of).

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply