String manipulating using substring-before problem

Discussion in 'XML' started by =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Oct 23, 2006.

  1. 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Oct 23, 2006
    #1
    1. Advertising

  2. 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
     
    Joe Kesselman, Oct 24, 2006
    #2
    1. Advertising

  3. 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Oct 24, 2006
    #3
  4. 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
     
    Joseph Kesselman, Oct 24, 2006
    #4
  5. 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
     
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Oct 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    19
    Views:
    12,922
    Netocrat
    Nov 10, 2005
  2. Sune
    Replies:
    0
    Views:
    88
  3. Replies:
    3
    Views:
    203
    Sherm Pendley
    Aug 3, 2005
  4. SM
    Replies:
    4
    Views:
    210
  5. BerlinBrown
    Replies:
    5
    Views:
    338
    BerlinBrown
    Feb 5, 2008
Loading...

Share This Page