Search code examples
xsltdita

XSLT: get relative URI instead of absolute uri


I want to:

  1. Create a file in the same folder where I am running my XSLT stylesheet against.
  2. This new file has a list of href to files with a certain value in the copyrholder element.
  3. Have a relative path in the href.

This is what I currently have:

  1. I create a new topic in the same folder
  2. List of href with absolute uri

Problem: make the absolute path relative to file I just created.

Example

This is the folder I am referencing to and all files have that particular element I want in the list:

C:/dita/file1.dita

C:/dita/file2.dita

C:/dita/file3.dita

C:/dita/file4.dita

C:/dita/en/file5.dita

This is the XSLT I use

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:template match="/">
<xsl:result-document href="newtopic.dita" doctype-public="-//OASIS//DTD DITA Topic//EN" doctype-system="topic.dtd" indent="yes">
<topic id="to_new_topics">
<xsl:element name="title">New topics</xsl:element>
<xsl:element name="body">
<xsl:variable name="folderURI" select="resolve-uri('.',base-uri())"/>
<ul>
<xsl:for-each select="collection(concat($folderURI, '?select=*.dita;recurse=yes'))//copyrholder[contains(., 'value')]">
<li>
<xsl:element name="xref">
<xsl:attribute name="href">
<xsl:value-of select="base-uri()" />
</xsl:attribute>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</xsl:element>
</topic>
</xsl:result-document>
</xsl:template>

</xsl:stylesheet>

This is the current result:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
<topic id="to_new_topics">
   <title>New topics</title>
   <body>
      <ul>
         <li><xref href="file:/C:/´dita/file1.dita"/></li>
         <li><xref href="file:/C:/´dita/file2.dita"/></li>
         <li><xref href="file:/C:/´dita/file3.dita"/></li>
         <li><xref href="file:/C:/´dita/file4.dita"/></li>
         <li><xref href="file:/C:/dita/en/file5.dita"/></li>
      </ul>
   </body>            
</topic>

This what I would like to have:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
<topic id="to_new_topics">
   <title>New topics</title>
   <body>
      <ul>
         <li><xref href="file1.dita"/></li>
         <li><xref href="file2.dita"/></li>
         <li><xref href="file3.dita"/></li>
         <li><xref href="file4.dita"/></li>
         <li><xref href="en/file5.dita"/></li>
      </ul>
   </body>            
</topic>

Anyone whou can help me to make the path relative?


Solution

  • Could this work:

    <xsl:value-of select="replace(base-uri(), $folderURI, '') "/>
    

    (did not see this at the comments), since if the problem is the absolute path at the xref, replacing the path with empty value should solve this problem. Or I just understand something terrible wrong. :) This is my tests end result:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE topic
      PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
    <topic id="to_new_topics">
       <title>New topics</title>
       <body>
          <ul>
             <li>
                <xref href="en/file3.dita"/>
             </li>
             <li>
                <xref href="file1.dita"/>
             </li>
             <li>
                <xref href="file2.dita"/>
             </li>
          </ul>
       </body>
    </topic>
    

    Tested with: XSL, newtopic.xml (to avoid problems and renamed this .xml, not .dita) and all file*.dita files are in the same folder or subfolder.