473,804 Members | 3,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT & String manipulation query

Hello,
I'm trying to perform some string manipulations in my stylesheet and
have gotten stuck on the issue below so hopefully can elicit some useful
hints.

Namely, the problem is that I need to convert an unqualified Xpath to
a fully qualified Xpath in an identity transform, i.e.

/AAA/BBB/CCC/@DDD

converted to

/ns:AAA/ns:BBB/ns:CCC/@DDD

with a predefined NS prefix and using a string tokenizer (adopted from
http://www.xslt.com/html/xsl-list/2005-04/msg00031.html) which returns
the tokens as:

<token>AAA</token>
<token>BBB</token>
<token>CCC</token>
<token>@DDD</token>

I'm assigning to the variable 'tokens' in the following template, which
then tries to produce the fully qualified namespace:

<xsl:template name="qualified Xpath">
<xsl:param name="unqualifi edXpath"/>
<!-- -->
<xsl:variable name="sampleUnq ualifiedXpath"
select="'/AAA/BBB/CCC/@DDD'"/>
<!-- hardcoded namespace prefix -->
<xsl:variable name="prefixStr ing" select="'dns:'"/>
<!-- hardcoded delimiter character -->
<xsl:variable name="slash" select="'/'"/>
<!-- Variable to contain the tokens -->
<xsl:variable name="tokens">
<!-- Calling tokenizer template-->
<xsl:call-template name="tokenizer ">
<xsl:with-param name="string" select="$sample UnqualifiedXpat h"/>
<xsl:with-param name="delimiter " select="$slash"/>
</xsl:call-template>
</xsl:variable>
<!-- Variable to hold the qualified Xpath -->
<xsl:variable name="qualXpath ">
<!-- Constructing the qualified Xpath-->
<!-- Iterate through the returned token nodes -->
<xsl:for-each select="$tokens/token">
<!-- Add delimiter-->
<xsl:value-of select="$slash"/>
<!-- Add prefix only when token is an element name i.e. doesn't have the
@ character -->
<xsl:if test="not(conta ins(.,'@'))">
<!-- Adding namespace prefix -->
<xsl:value-of select="$prefix String"/>
<!-- Add token's value -->
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- returning qualXpath variable -->
<xsl:value-of select="$qualXp ath"/>
</xsl:template>

where the tokens' usage in the loop declaration
<xsl:for-each select="$tokens/token">

is causing a Result Tree Fragment (RTF) error; I've googled to find out
more about it but frankly don't understand the problem, and unsure what
an alternative solution would be and hence seeking Wisdom of The Elders!
:-)

BTW, the code snippet above might be buggy (hopefully not too much) as
I've been stuck at this RTF error and hence unable to proceed. I suspect
the usage of contains(.,'@') ,
where I intend to check the token node's value for a '@' character, is
bit fishy. :)

Many thanks!

Regards,

Bilal B.

*** Sent via Developersdex http://www.developersdex.com ***
Oct 6 '06 #1
10 6050
In article <45************ ***********@new s.qwest.net>,
Bilal <no****@devdex. comwrote:
><xsl:variabl e name="tokens">
<!-- Calling tokenizer template-->
<xsl:call-template name="tokenizer ">
<xsl:with-param name="string" select="$sample UnqualifiedXpat h"/>
<xsl:with-param name="delimiter " select="$slash"/>
</xsl:call-template>
</xsl:variable>
Here you construct a result-tree fragment - the value of the variable
"tokens" is not a nodeset from the original document, but a
constructed nodeset.

In XSLT 1.0 you can't do much with result-tree fragments. In particular
you can't do this sort of thing:
><xsl:for-each select="$tokens/token">
Many XSLT processors have an extension function that allows you to convert
a result-tree fragment into an ordinary nodeset. For example, the
exsl:node-set() function may be available, see

http://www.exslt.org/exsl/functions/node-set/

-- Richard
Oct 6 '06 #2
As Richard implied, XSLT 2.0 (when it becomes official and more widely
available) removes that distinction between nodesets and result-tree
fragments.

If you don't want to rely on the extension function, the other 1.0
solution is to rewrite the tokenize-and-reconstruct process so it yields
the new string directly, rather than producing an RTF and then walking
that to produce the string. There are examples of recursive substring
replacement on the XSLT FAQ website, among many other places; it's a
fairly common idiom for functional languages like XSLT.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Oct 6 '06 #3
Hi Richard & Joe,
Thanks for the explanation and suggestions. Joe, based on your
suggestion, I started exploring other solution and I think I've come
across a search-and-replace method in my XSLT cookbook which does this
job. Now time to understand and customize it! ;-)

BTW, I'm sure I'm not the only one with procedural language experience
who has trouble with this functional language; its seems like a whole
different beast!

Many thanks for the help guys!

Regards,

Bilal B.

*** Sent via Developersdex http://www.developersdex.com ***
Oct 6 '06 #4
This transformation:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:testmap=" testmap"
exclude-result-prefixes="xsl testmap"
>
<xsl:import href="str-map.xsl"/>

<!-- to be applied on any xml source -->

<testmap:testma p/>

<xsl:output omit-xml-declaration="ye s" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="vTestMap" select="documen t('')/*/testmap:*[1]"/>
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vTestM ap"/>
<xsl:with-param name="pStr"
select="substri ng-before('/AAA/BBB/CCC/@DDD', '/@')"/>
</xsl:call-template>
<xsl:value-of select=
"concat('/@', substring-after('/AAA/BBB/CCC/@DDD', '/@'))"/>
</xsl:template>

<xsl:template name="double" match="testmap: *">
<xsl:param name="arg1"/>

<xsl:value-of select="$arg1"/>
<xsl:if test="$arg1 = '/'">
<xsl:value-of select="'ns:'"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

when applied on any source xml document (not used),

produces the wanted result:

/ns:AAA/ns:BBB/ns:CCC/@DDD
Cheers,
Dimitre Novatchev

"Bilal" <no****@devdex. comwrote in message
news:45******** *************** @news.qwest.net ...
Hello,
I'm trying to perform some string manipulations in my stylesheet and
have gotten stuck on the issue below so hopefully can elicit some useful
hints.

Namely, the problem is that I need to convert an unqualified Xpath to
a fully qualified Xpath in an identity transform, i.e.

/AAA/BBB/CCC/@DDD

converted to

/ns:AAA/ns:BBB/ns:CCC/@DDD

with a predefined NS prefix and using a string tokenizer (adopted from
http://www.xslt.com/html/xsl-list/2005-04/msg00031.html) which returns
the tokens as:

<token>AAA</token>
<token>BBB</token>
<token>CCC</token>
<token>@DDD</token>

I'm assigning to the variable 'tokens' in the following template, which
then tries to produce the fully qualified namespace:

<xsl:template name="qualified Xpath">
<xsl:param name="unqualifi edXpath"/>
<!-- -->
<xsl:variable name="sampleUnq ualifiedXpath"
select="'/AAA/BBB/CCC/@DDD'"/>
<!-- hardcoded namespace prefix -->
<xsl:variable name="prefixStr ing" select="'dns:'"/>
<!-- hardcoded delimiter character -->
<xsl:variable name="slash" select="'/'"/>
<!-- Variable to contain the tokens -->
<xsl:variable name="tokens">
<!-- Calling tokenizer template-->
<xsl:call-template name="tokenizer ">
<xsl:with-param name="string" select="$sample UnqualifiedXpat h"/>
<xsl:with-param name="delimiter " select="$slash"/>
</xsl:call-template>
</xsl:variable>
<!-- Variable to hold the qualified Xpath -->
<xsl:variable name="qualXpath ">
<!-- Constructing the qualified Xpath-->
<!-- Iterate through the returned token nodes -->
<xsl:for-each select="$tokens/token">
<!-- Add delimiter-->
<xsl:value-of select="$slash"/>
<!-- Add prefix only when token is an element name i.e. doesn't have the
@ character -->
<xsl:if test="not(conta ins(.,'@'))">
<!-- Adding namespace prefix -->
<xsl:value-of select="$prefix String"/>
<!-- Add token's value -->
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- returning qualXpath variable -->
<xsl:value-of select="$qualXp ath"/>
</xsl:template>

where the tokens' usage in the loop declaration
<xsl:for-each select="$tokens/token">

is causing a Result Tree Fragment (RTF) error; I've googled to find out
more about it but frankly don't understand the problem, and unsure what
an alternative solution would be and hence seeking Wisdom of The Elders!
:-)

BTW, the code snippet above might be buggy (hopefully not too much) as
I've been stuck at this RTF error and hence unable to proceed. I suspect
the usage of contains(.,'@') ,
where I intend to check the token node's value for a '@' character, is
bit fishy. :)

Many thanks!

Regards,

Bilal B.

*** Sent via Developersdex http://www.developersdex.com ***

Oct 7 '06 #5
Of course, the imported stylesheet is from the EXSLT-version of FXSL1.x

When one has such a toolset, solving such problems takes just two minutes.

Cheers,
Dimitre Novatchev

"Dimitre Novatchev" <di******@tpg.c om.auwrote in message
news:45******** *************** @authen.yellow. readfreenews.ne t...
This transformation:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:testmap=" testmap"
exclude-result-prefixes="xsl testmap"
>>
<xsl:import href="str-map.xsl"/>

<!-- to be applied on any xml source -->

<testmap:testma p/>

<xsl:output omit-xml-declaration="ye s" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="vTestMap" select="documen t('')/*/testmap:*[1]"/>
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vTestM ap"/>
<xsl:with-param name="pStr"
select="substri ng-before('/AAA/BBB/CCC/@DDD', '/@')"/>
</xsl:call-template>
<xsl:value-of select=
"concat('/@', substring-after('/AAA/BBB/CCC/@DDD', '/@'))"/>
</xsl:template>

<xsl:template name="double" match="testmap: *">
<xsl:param name="arg1"/>

<xsl:value-of select="$arg1"/>
<xsl:if test="$arg1 = '/'">
<xsl:value-of select="'ns:'"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

when applied on any source xml document (not used),

produces the wanted result:

/ns:AAA/ns:BBB/ns:CCC/@DDD
Cheers,
Dimitre Novatchev

"Bilal" <no****@devdex. comwrote in message
news:45******** *************** @news.qwest.net ...
>Hello,
I'm trying to perform some string manipulations in my stylesheet and
have gotten stuck on the issue below so hopefully can elicit some useful
hints.

Namely, the problem is that I need to convert an unqualified Xpath to
a fully qualified Xpath in an identity transform, i.e.

/AAA/BBB/CCC/@DDD

converted to

/ns:AAA/ns:BBB/ns:CCC/@DDD

with a predefined NS prefix and using a string tokenizer (adopted from
http://www.xslt.com/html/xsl-list/2005-04/msg00031.html) which returns
the tokens as:

<token>AAA</token>
<token>BBB</token>
<token>CCC</token>
<token>@DDD</token>

I'm assigning to the variable 'tokens' in the following template, which
then tries to produce the fully qualified namespace:

<xsl:templat e name="qualified Xpath">
<xsl:param name="unqualifi edXpath"/>
<!-- -->
<xsl:variabl e name="sampleUnq ualifiedXpath"
select="'/AAA/BBB/CCC/@DDD'"/>
<!-- hardcoded namespace prefix -->
<xsl:variabl e name="prefixStr ing" select="'dns:'"/>
<!-- hardcoded delimiter character -->
<xsl:variabl e name="slash" select="'/'"/>
<!-- Variable to contain the tokens -->
<xsl:variabl e name="tokens">
<!-- Calling tokenizer template-->
<xsl:call-template name="tokenizer ">
<xsl:with-param name="string" select="$sample UnqualifiedXpat h"/>
<xsl:with-param name="delimiter " select="$slash"/>
</xsl:call-template>
</xsl:variable>
<!-- Variable to hold the qualified Xpath -->
<xsl:variabl e name="qualXpath ">
<!-- Constructing the qualified Xpath-->
<!-- Iterate through the returned token nodes -->
<xsl:for-each select="$tokens/token">
<!-- Add delimiter-->
<xsl:value-of select="$slash"/>
<!-- Add prefix only when token is an element name i.e. doesn't have the
@ character -->
<xsl:if test="not(conta ins(.,'@'))">
<!-- Adding namespace prefix -->
<xsl:value-of select="$prefix String"/>
<!-- Add token's value -->
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- returning qualXpath variable -->
<xsl:value-of select="$qualXp ath"/>
</xsl:template>

where the tokens' usage in the loop declaration
<xsl:for-each select="$tokens/token">

is causing a Result Tree Fragment (RTF) error; I've googled to find out
more about it but frankly don't understand the problem, and unsure what
an alternative solution would be and hence seeking Wisdom of The Elders!
:-)

BTW, the code snippet above might be buggy (hopefully not too much) as
I've been stuck at this RTF error and hence unable to proceed. I suspect
the usage of contains(.,'@') ,
where I intend to check the token node's value for a '@' character, is
bit fishy. :)

Many thanks!

Regards,

Bilal B.

*** Sent via Developersdex http://www.developersdex.com ***


Oct 7 '06 #6
Hi Dimitre,
Thanks for your input; your first reply seemed interesting, but as you
pointed out it using EXSLT, which I can't use just yet as I'm limited to
XSLT 1.0 only.

Regards,

Bilal B.
*** Sent via Developersdex http://www.developersdex.com ***
Oct 9 '06 #7

"Bilal" <no****@devdex. comwrote in message
news:45******** *************** @news.qwest.net ...
Hi Dimitre,
Thanks for your input; your first reply seemed interesting, but as you
pointed out it using EXSLT, which I can't use just yet as I'm limited to
XSLT 1.0 only.

Regards,

Bilal B.
Then you could use one of the three vendor-dependent versions of FXSL
1.x -- MSXML, Saxon or Xalan- dependent.

Cheers,
Dimitre Novatchev
Oct 10 '06 #8
Xalan does implement the EXSLT library, I believe.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Oct 10 '06 #9
I understood him to say he was not ready yet to use EXSLT, not that his XSLT
processor didn't support it.

Cheers,
Dimitre Novatchev
"Joe Kesselman" <ke************ @comcast.netwro te in message
news:pp******** *************** *******@comcast .com...
Xalan does implement the EXSLT library, I believe.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry

Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5708
by: J Trost | last post by:
I was wondering if anyone knows if it is possible to do basic string replacement using XSLT even though the strings being replaced may contain "<" and ">". Here is my problem: I need to be able to convert XML like this: <?xml version="1.0" encoding="UTF-8"?> <java version="1.4.2_03" class="java.beans.XMLDecoder"> <object class="javax.swing.JButton"> <string>Hello, world</string>
5
16886
by: Per Johansson | last post by:
Is it possible to use XSLT to automatically create href links while it formats an XML document? That is, if it finds "http://me.us/" in a text, it adds <a href="http://me.us/">http://me.us/</a> -- Per Johansson Systems developer http://per.johansson.name/
2
5288
by: chris | last post by:
Hi there, I create an XML file from a dataset like this: System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(FILENAME); dsUserData1.WriteXml(xmlSW, XmlWriteMode.WriteSchema); xmlSW.Close(); Which gives me this XML file: <NewDataSet>
9
3422
by: Ray | last post by:
I need to convert the normal calendar to show the week no., the period no. and the financial year. The financial year format is as follows:- Date start: 2 May, 2005 7 days a week, 4 weeks a period and 13 periods a year. normally 52 weeks per year but one 53-week a year every 6 years. The 53th week is included in period 13. Can someone advise any idea how to construct such conversion.
1
1647
by: jrwarwick | last post by:
Hello, I believe I have uncovered a bug in the .Net XSLT engine to do with 'for' loops in XSLT. Here are the steps to reproduce it: -Create A new webform project. -Add the xml file 'errorexample.xml' - it is shown at the bottom -Add the xslt file 'errorexample.xslt' - it is shown at the bottom -Add a label into the WebForm1.aspx designer and stretch it so it is large, give it the ID of Output1. -Import the following namespaces in...
4
1930
by: shaun roe | last post by:
I should like to count the frequency of strings embedded in a longer string, space separated. Specifically, I have: <phiModule> 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 8 5 5 5 6 6 6 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 9 9 9 6 7 7 7 8 8 8 8 9 9 9
0
13759
NeoPa
by: NeoPa | last post by:
Intention : To prepare a WHERE clause for multiple field selection, but to ignore any fields where the selection criteria are not set. ONLY WORKS WITH TEXT FIELD SELECTIONS. Scenario : You have a table (tblMember) containing information for various people. Table Name=tblMember Field; Type; IndexInfo MemberID; AutoNumber; PK Surname; String
4
11958
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report> </myXml> I there a way that the XSLT transformation can render the content as html rather than text?
11
4571
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" /> and so on. Let's suppose I want to retrieve this @url parameter, BUT ONLY with the values, in querystring, associated with "foo" and "foo2" (thus discarding name1, name2, name4 and every other different ones).
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.