By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

String manipulating using substring-before problem

P: n/a
Hello,

I've been looking at this for a bit now and I don't see what's wrong
with the code. Can anybody see a problem with this?

Here is an XSLT snippet I use.

<xsl:template match="graphic">
<xsl:param name="path" />
<fo:block padding-after="12pt">
<xsl:variable name="xml-name">
<xsl:value-of select="substring-before(substring-after(@source,
'/'), '_Images')"/>.xml
</xsl:variable>
<xsl:variable name="image-path-name">
<xsl:value-of select="substring-after(@source, '/')" />
</xsl:variable>
<xsl:variable name="graphic-path">
<xsl:value-of select="substring-before($path, $xml-name)"
/><xsl:value-of select="$image-path-name" />
</xsl:variable>
<fo:block><xsl:value-of select="$path" /></fo:block>
<fo:block><xsl:value-of select="$xml-name" /></fo:block>
<fo:block><xsl:value-of select="$image-path-name" /></fo:block>
<fo:block><xsl:value-of select="substring-before($path,
$xml-name)" /></fo:block>
<fo:external-graphic src="url({$graphic-path})"/>
<xsl:value-of select="$graphic-path" />
</fo:block>
</xsl:template>

This is the output on the PDF.

../User_Guide_XML/Applicability_Menu/Insert_Applicability.xml
Insert_Applicability.xml
Insert_Applicability_Images/Fig-3.jpg
Insert_Applicability_Images/Fig-3.jpg

You would think that substring-before($path, $xml-name) would give me

"./User_Guide_XML/Applicability_Menu/", but it doesn't seem to work. If
I replace $xml-name in substring-before with an actual string, it seems
to work. What am I missing here? I was using 2 variables before trying
this trick in the substring-before and it didn't cause any problems
then. What's changed?

This is a snippet of the input XML (path contains the section id
attribute value:

<section id="./User_Guide_XML/File_Menu/Close_All_Documents.xml">
<title>
<bold>Closing All Documents</bold>
</title>

<graphic source="./Close_All_Documents_Images/Fig-1.jpg"/>
</section>

Regards
Jean-Francois Michaud

Oct 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Start by trying a simpler test, in isolation:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="path"
select="'./User_Guide_XML/Applicability_Menu/Insert_Applicability.xml'"/>
<xsl:variable name="xml-name" select="'Insert_Applicability.xml'"/>
<xsl:variable name="image-path-name"
select="'Insert_Applicability_Images/Fig-3.jpg'"/>
<xsl:variable name="graphic-path">
<xsl:value-of select="substring-before($path, $xml-name)"
/><xsl:value-of select="$image-path-name" />
</xsl:variable>
path: <xsl:value-of select="$path" />
xml-name: <xsl:value-of select="$xml-name" />
image-path-name: <xsl:value-of select="$image-path-name" />
s-b(): <xsl:value-of select="substring-before($path,$xml-name)" />
graphic-path: <xsl:value-of select="$graphic-path" />
</xsl:template>
</xsl:stylesheet>

Note that this is completely independent of your input document, so it
will check the substring-before logic.

Output from Xalan:

<?xml version="1.0" encoding="UTF-8"?>
path: ./User_Guide_XML/Applicability_Menu/Insert_Applicability.xml
xml-name: Insert_Applicability.xml
image-path-name: Insert_Applicability_Images/Fig-3.jpg
s-b(): ./User_Guide_XML/Applicability_Menu/
graphic-path:
../User_Guide_XML/Applicability_Menu/Insert_Applicability_Images/Fig-3.jpg

.... which seems to be what you expected.


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Oct 24 '06 #2

P: n/a

Joe Kesselman wrote:
Start by trying a simpler test, in isolation:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="path"
select="'./User_Guide_XML/Applicability_Menu/Insert_Applicability.xml'"/>
<xsl:variable name="xml-name" select="'Insert_Applicability.xml'"/>
<xsl:variable name="image-path-name"
select="'Insert_Applicability_Images/Fig-3.jpg'"/>
<xsl:variable name="graphic-path">
<xsl:value-of select="substring-before($path, $xml-name)"
/><xsl:value-of select="$image-path-name" />
</xsl:variable>
path: <xsl:value-of select="$path" />
xml-name: <xsl:value-of select="$xml-name" />
image-path-name: <xsl:value-of select="$image-path-name" />
s-b(): <xsl:value-of select="substring-before($path,$xml-name)" />
graphic-path: <xsl:value-of select="$graphic-path" />
</xsl:template>
</xsl:stylesheet>

Note that this is completely independent of your input document, so it
will check the substring-before logic.

Output from Xalan:

<?xml version="1.0" encoding="UTF-8"?>
path: ./User_Guide_XML/Applicability_Menu/Insert_Applicability.xml
xml-name: Insert_Applicability.xml
image-path-name: Insert_Applicability_Images/Fig-3.jpg
s-b(): ./User_Guide_XML/Applicability_Menu/
graphic-path:
./User_Guide_XML/Applicability_Menu/Insert_Applicability_Images/Fig-3.jpg

... which seems to be what you expected.
Arumph!!! I knew I wasn't crazy hehehe. I don't know what the problem
lies then. I'll have to keep looking. Thanks for looking into it ;-).
I'm thinking maybe a run-time problem? Maybe the string doesn't exist
in the form I want until after everything has been processed which
would result in the string not outputting like I want?

In my example, xml-name is a constructed string. Path is a real string
and so is @source.

As a result for "substring-before($path,$xml-name)", I get an empty
string in my bit of code!!!
If I replace $xml-name with an actual string, It works. But then again,
I was using this construct before using lesser logic and it was
working. The final string wouldn't come empty which tend to mean that
it's NOT a run-time string problem. Hmmm.

Regards
Jean-Francois Michaud

Oct 24 '06 #3

P: n/a
Doublecheck your actual data for problems like whitespace which may not
be obvious when you just print them out.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Oct 24 '06 #4

P: n/a

Joseph Kesselman wrote:
Doublecheck your actual data for problems like whitespace which may not
be obvious when you just print them out.
Bah, it was a whitespace problem. In the xslt, I put parenthesis inline
within the blocks to see how long the strings were. The xml-name string
had a single whitespace at the end. The problem wasn't with the data
though, it was with the xslt. Manual formatting played against me.
BAH!!

Thanks for your help ;-).

Regards
Jean-Francois Michaud

Oct 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.