xsl-list
[Top] [All Lists]

Re: [xsl] another beginner question - strip directory info from variable

2018-10-14 03:12:50
Dave --

I really like "tokenize( $filepath, '/')[last()]" for this. You may
well have tried that very thing, but the problem is you are handing
the tokenize() function a sequence of multiple filepaths, not just a
single filepath. To deal with this, you probably want to process the
sequence of filepaths such that you are only handling one at a time.
I don't know what is the *best* way to do this, but here are three
that all work.

--------- begin demo XSLT program ---------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:math="http://www.w3.org/2005/xpath-functions/math";
  exclude-result-prefixes="xs math"
  version="3.0">
  
  <!-- tell processor I just what Unicode text, not XML, as output -->
  <xsl:output method="text"/>
  <!-- set up test variable; allow user to change via parameter if she wants -->
  <xsl:param name="jpgs" select="('dir1/jpg001.jpg', 'dir1/jpg002.jpg', 
'dir1/jpg003.jpg')"/>
  
  <!-- Just match the document and perform test — thus it does not matter what 
the
  input file is, as we never even look at it. -->
  <xsl:template match="/">
    <xsl:text>Method 1, xsl:for-each:&#x0A;</xsl:text>
    <xsl:for-each select="$jpgs">
      <xsl:text> * JPG#</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text> is </xsl:text>
      <xsl:value-of select="tokenize( ., '/')[last()]"/>
      <xsl:text>&#x0A;</xsl:text>
    </xsl:for-each>
    <xsl:text>&#x0A;Method 2, XPath for loop:&#x0A;</xsl:text>
    <xsl:variable name="jpgs_sans_dirs" select="for $j in $jpgs return 
tokenize($j,'/')[last()]"/>
    <!-- Now use xsl:for-each just to print them out nicely -->
    <xsl:for-each select="$jpgs_sans_dirs">
      <xsl:text> - JPG#</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text> is </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#x0A;</xsl:text>
    </xsl:for-each>
    <xsl:text>&#x0A;Method 3, xsl:iterate:&#x0A;</xsl:text>
    <xsl:iterate select="$jpgs">
      <xsl:text> + JPG#</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text> is </xsl:text>
      <xsl:value-of select="tokenize( ., '/')[last()]"/>
      <xsl:text>&#x0A;</xsl:text>
    </xsl:iterate>    
  </xsl:template>
  
</xsl:stylesheet>
--------- end demo XSLT program ---------

Hi again everyone - back with another beginner question.

I'm working in Oxygen and am using Saxon.

I'm searching through a bunch of xml files in search of jpg names. I'm 
creating variables as follows:

<xsl:variable name="allxml" 
select="collection('..//xml/?select=*.xml;recurse=yes')"/>
<xsl:variable name="jpgs" select="$allxml//element/@n"/>

$jpgs looks like:

dir1/jpg001.jpg dir1/jpg002.jpg dir1/jpg003.jpg

etc.

I want to strip away the dir info, but am having a hard time. I've tried 
using tokenize and substring-after but both return similar errors.

"A sequence of more than one item is not allowed as the first argument 
of fn:tokenize() ("dir1/jpg001.jpg", "dir1/jpg002.jpg")."

Because I don't know what I'm doing, I don't know what I'm doing wrong. :)

Can anyone help me figure out how to solve this problem?
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>