Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2011, 03:58 AM
Registered User
 
Join Date: Aug 2011
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default unparsed-entity-uri() w/o path

I am using unparsed-entity-uri() to resolve graphic entities, but only want the filename without the full path, as the graphics on the target server are in a different path.

I'm able to get the desired result, but have a nagging suspicion that there has to be an easier way to do this, maybe without recursion. Any suggestions?

Source is something like this:

Code:
<?xml version="1.0"?>
<!DOCTYPE cmm [
  <!ENTITY graphic1 SYSTEM "c:\graphics\some_folder\abcd.tif" NDATA ccitt4>
]>
<cmm>
  <sheet gnbr="graphic1"/>
</cmm>
XSL is something like this:
Code:
  <xsl:template match="/">
    <body><xsl:apply-templates/></body>
  </xsl:template>

  <xsl:template match="sheet">
    <xsl:variable name="imgref" select="unparsed-entity-uri(@gnbr)"/>
    <xsl:element name="img">
      <xsl:attribute name="href">
        <xsl:call-template name="getFilename">
          <xsl:with-param name="uri" select="translate($imgref,'\','/')"/>
        </xsl:call-template>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template name="getFilename">
    <xsl:param name="uri"/>
    <xsl:choose>
      <xsl:when test="contains($uri,'/')">
        <xsl:call-template name="getFilename">
          <xsl:with-param name="uri" select="substring-after($uri,'/')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$uri"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
Result is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<body>
   <img href="abcd.tif"/>
</body>
 
Old August 31st, 2011, 05:14 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The value of unparsed-entity-uri() is supposed to be an absolute URI, which means it should never contain a backslash. That's the theory, at any rate. In practice, of course, products sometimes treat Windows filenames as if they were URIs, so it might be prudent to allow it.

There are much easier ways to do this in XSLT 2.0, but if you're stuck with 1.0, then your code is the right way to do it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
nwebb (September 1st, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
absolute path vs relative path for substitution group elements ptn77 XSLT 3 June 16th, 2011 10:04 AM
list unparsed-entity-uri() bonekrusher XSLT 6 April 28th, 2008 10:52 AM
unparsed-entity-uri() changes files extension scopley XSLT 6 November 28th, 2007 10:45 AM
Difference between Entity and Entity type arshad mahmood C++ Programming 0 May 8th, 2004 12:34 AM
Referrer and Uri druid2112 General .NET 1 April 20th, 2004 12:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.