Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XSLT URI string manipulation

Posted on 2004-08-25
7
Medium Priority
?
784 Views
Last Modified: 2012-08-14
I do some string manipulation on a XML field (cs-uri-stem) and display the results thus:

    <xsl:variable name="hno"  select="substring-before(  substring-after(substring-after( substring-after( substring-after(substring-after( substring-after( cs-uri-stem , '/') , '/') , '/') , '/') , '/'), '/'), '/') "/>
    <xsl:value-of select='translate($hno,"-","/")' />


Sometimes the string is not in the correct format (seven slashes), and in this case nothing is displayed.


Seeing as though I am manipulating a URI string and I only want the last part of it, is there a better way of stripping out the preceding part of the string rather than using substring-before?

At worst, is there a way of doing an if-then-else type of operation that will display the contents of cs-uri-stem is the variable $hno is empty?

Thanks.
0
Comment
Question by:colly92002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 11894155
Well, you could test for the slashes with an xsl:choose structure, using contains(nodeName, '/') as the test.

If you can use extensions, you could create a regular expression extension function to do this.  If not, then I'd recursively process each character in the string, doing whatever I needed to do.  Here's an example:

http://rdcpro.com/Members/rdcpro/snippets/textparser/

Regards,
Mike Sharp
0
 
LVL 15

Author Comment

by:colly92002
ID: 11894712
Thanks.  I'm sure you have answered this for me before!

I shall look into this next week (on holiday now).
0
 
LVL 15

Author Comment

by:colly92002
ID: 11990269
Hello again.

Your example hasn't really helped me I'm afraid, its doing what I want to do in reverse, and rather thatn trying to re-code it, I think I would rather learn about extensions.

I have had a quick look at www.exslt.org and the match extensions seems to be what I am looking for.  However, I cannot get it to work on my server (maily due to me rushing in feet first and not learning what I have to do).

Can you point me in the direction of a good tutorial for creating/using extensions?

Here is how I have tried to include the extension:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:func="http://exslt.org/functions"
        xmlns:regexp="http://exslt.org/regular-expressions"
        extension-element-prefixes="regexp"
>
<func:script implements-prefix="regexp"
      language="exslt:javascript"
      src="regexp.xslt" />

When I use it:
<xsl:choose>
   <xsl:when test="function-available('regexp:match')">
    <TD class="char">
      <xsl:for-each select="regexp:match('This is a test string', '(\w+)', 'g'">
         Part <xsl:value-of select="position()" /> = <xsl:value-of select="." />
      </xsl:for-each>
    </TD>
   </xsl:when>
   <xsl:otherwise>


I get:

msxml3.dll error '80004005'

Expression expected. regexp:match('This is a test string', '(\w+)', 'g'<--

/OnlineApplications/library/asp/xmlutil.asp, line 37

(N.B. line 37 is the transform:
      fn_xmlutil_TransformXML = objXML.TransformNode(objXSL)
)

I don't think I am including it correctly.  I have tried various options for the "src" value, but none of them work.

Any more help greatly appreciated.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Accepted Solution

by:
rdcpro earned 1000 total points
ID: 11991143
Msxml 3 doesn't easily support EXSLT.  It can be done, but it's not easy.  There was an article on it, perhaps on exslt.org that gave instructions on supporting EXSLT from the MSXML parser...but I can't remember where it was that I saw it.

You can, however, borrow or write your own extension. For example, here's a date extension script I used once in a class demo:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                        xmlns:dates="urn:rdcpro-com:dates"
                        >
      <xsl:output method="html" encoding="UTF-8"/>
      <msxsl:script language="JScript" implements-prefix="dates">
            <![CDATA[
                  function parseDate(oNodeList) {
                  var dateString = oNodeList[0].text;
                  var t = Date.parse(dateString);
                  return t;
                  }
                  function formatDate(t) {
                  var oDate = new Date(t);
                  return oDate.getFullYear() + "-" + (oDate.getMonth() + 1) + "-" + oDate.getDate();
                  }
            ]]>
      </msxsl:script>

The idea is that you use the regex match function in your code, to do whatever you want.  Bear in mind, if you later end up using this in .NET, that extensions are considered a security risk, and in some cases code access security will require your assembly to run with elevated privileges, or else be run from the GAC.  That's why lately I've preferred pure XSLT solutions, like my recursive text parser that I pointed you towards.

You access the script function above like:

<xsl:value-of select="dates:parseDate(StartDate)"/>

which produces a sortable date string in this example.  So borrow the EXSLT javascript code, plug it into your stylesheet directly, and away you go.

Regards,
Mike Sharp
0
 
LVL 15

Author Comment

by:colly92002
ID: 11991241
That should do it for me.

I don't understand why I get msxml3.dll error, I thought I had msxml4 installed!  However, this is a production server that uses MSXML all the time so I don't want to go messing about with it now.  I shall make sure my new server is running MSXML 4.

Is it possible to "include" your above example ( like a SSI in HTML ?) so I could build up a library of code like this using MSXML3?  I always assumed that this would be easy, but the only way I can see to do it is using extensions (as per my example above that doesn't work) ?

Thanks.
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11991449
Well, you can include and import XSLT documents.  That would work for you, I think. Check out xsl:include and xsl:import.  You can also use Extension Objects in XSLT, so you could create a series of classes that did what you wanted.  This is a bit harder, but might be worth it if you're doing this on a large scale.

To use MSXML 4, you must be sure to use the correct ProgId:  "Msxml2.DomDocument.4.0" or "Msxml2.FreeThreadedDomDocument.4.0"

There are similar ProgIds for the XSLTemplate processor.  The version-independant ProgIds are always going to use MSXML 3, if it's installed, and MSXML 2, if not.

Regards,
Mike Sharp
0
 
LVL 15

Author Comment

by:colly92002
ID: 11991867
Thanks for that.  The ProgId is at fault!  I set the whole thing up from scratch from a position of learning everything, and I was using the old namespace for my XSLT definitions (microsoft examples!).

I shall fix all that for the next release.

You have been of great help thank you very much!  I have been using XML/XSLT now for 2 years and have set up a full reporting system for used by five data analyst, and yet I am still just a rookie at it!  Experts Exchange has helped me so much.

Thanks again.

Iain.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question