Ottenere la directory di foglio di stile all'interno del file-XSL per la posizione xml configureable

StackOverflow https://stackoverflow.com/questions/2348836

  •  23-09-2019
  •  | 
  •  

Domanda

Ho un file XSL per un XML. La posizione dei file XML dovrebbe essere configurabile (questo è fatto configurando il percorso href al foglio di stile xml), ma l'XSL è con alcune immagini e alcuni file JavaScript Othe e la necessità di hav teh percorso a loro. Il percorso è proprio vicino al foglio di stile così una volta posso ottenere la directory xsl posso ge a loro. per esempio: nel mio xml ho:? tipo xml-stylesheet = "text / xsl" href = "\ Files \ Style \ Test.xsl."> Voglio dall'interno del xsl per puntare a "\ Files \ Style" per la posizione delle immagini Hoe posso fare questo

È stato utile?

Soluzione

Ecco un XSLT 1.0 soluzione (XSLT 2.0 ha molto più potenti funzionalità per l'elaborazione delle stringhe, come ad esempio le espressioni regolari):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="processing-instruction()">
   <xsl:variable name="vpostHref"
    select="substring-after(., 'href=')"/>

   <xsl:variable name="vhrefData1"
    select="substring($vpostHref,2)"/>

   <xsl:variable name="vhrefData2"
    select="substring($vhrefData1, 1,
                      string-length($vhrefData1)-1
                      )"/>

   <xsl:call-template name="stripBackwards">
    <xsl:with-param name="pText"
      select="$vhrefData2"/>
    <xsl:with-param name="pTextLength"
     select="string-length($vhrefData2)"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="stripBackwards">
  <xsl:param name="pText"/>
  <xsl:param name="pStopChar" select="'\'"/>
  <xsl:param name="pTextLength"/>

  <xsl:choose>
   <xsl:when test="not(contains($pText, $pStopChar))">
     <xsl:value-of select="$pText"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:variable name="vLastChar"
       select="substring($pText,$pTextLength,1)"/>
     <xsl:choose>
       <xsl:when test="$vLastChar = $pStopChar">
        <xsl:value-of select="substring($pText,1,$pTextLength -1)"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:call-template name="stripBackwards">
          <xsl:with-param name="pText"
           select="substring($pText,1,$pTextLength -1)"/>
          <xsl:with-param name="pTextLength" select="$pTextLength -1"/>
          <xsl:with-param name="pStopChar" select="$pStopChar"/>
        </xsl:call-template>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

Quando si applica questa trasformazione sul seguente documento XML :

<?xml-stylesheet type="text/xsl" href=".\Files\Style\test.xsl"?>
<t/>

il risultato corretto è prodotto :

.\Files\Style
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top