2

Is it possible to get data after second symbol ;? For example I have this XML:

<document>
    <line>H;1F;ss;232</line>
    <line>H2;1F;sss;232e</line>
    <line>H;15F5;sds;232sa;23</line>
    <line>Hh;1Fs;scs;232ds</line>
</document>

result should be:

<document>
    <line>ss</line>
    <line>sss</line>
    <line>sds</line>
    <line>scs</line>
</document>
3
  • Good question, +1. See my answer for two solutions: an XSLT 1.0 solution using FXSL and an XSLT 2.0 solution. Extensive explanation and a link to FXSL is provided. Jul 26, 2011 at 13:04
  • Was my answer useful to you.? Or did you have any problems applying the solution? If yes, what are these problems? Jul 31, 2011 at 16:58
  • @Dimitre Novatchet: thanks for answer, it is what I need. because i have about 10 symbols ";" and it's difficult to find it with substring-before and after.
    – Petras
    Aug 1, 2011 at 14:44

2 Answers 2

2

I. An XSLT 1.0 solution using FXSL:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
  exclude-result-prefixes="ext">
   <xsl:import href="strSplit-to-Words.xsl"/>
   <xsl:output indent="yes" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
     <document>
      <xsl:apply-templates/>
     </document>
    </xsl:template>

    <xsl:template match="line">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="."/>
          <xsl:with-param name="pDelimiters" 
                          select="';'"/>
        </xsl:call-template>
      </xsl:variable>

      <line>
       <xsl:value-of select="ext:node-set($vwordNodes)/*[3]"/>
      </line>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<document>
    <line>H;1F;ss;232</line>
    <line>H2;1F;sss;232e</line>
    <line>H;15F5;sds;232sa;23</line>
    <line>Hh;1Fs;scs;232ds</line>
</document>

produces the wanted, correct result:

<document>
   <line>ss</line>
   <line>sss</line>
   <line>sds</line>
   <line>scs</line>
</document>

Explanation:

  1. We use the str-split-to-words template from the FXSL library to split the string value of every line element.. Note that it allowis not only a single character, but multiple characters to be specified in its pDelimiters parameter. In this case we specify only ';'.

  2. The result is captured in a variable as an RTF. We convert it to ordinary tree using the ext:node-set() extension function. If your XSLT processor doesn't implement ext:node-set() look in its documentation what is the name and namespace of the xxx:node-set() extension that this particular XSLT processor implements.

  3. From the thus obtained tree, we only select and output the 3rd element. Note that this is a short XPath expression and the same short XPath expression would be used even if we wanted the 20th element -- just the index would be 20. This is impossible to achieve in a short and neat form using just a series of substring-after() functions.

II. Here is a short and easy XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
     <document>
      <xsl:apply-templates/>
     </document>
 </xsl:template>

 <xsl:template match="line">
  <document>
   <xsl:value-of select="tokenize(., ';')[3]"/>
  </document>
 </xsl:template>
</xsl:stylesheet>

when this transformation is aplied on the provided XML document:

<document>
    <line>H;1F;ss;232</line>
    <line>H2;1F;sss;232e</line>
    <line>H;15F5;sds;232sa;23</line>
    <line>Hh;1Fs;scs;232ds</line>
</document>

the wanted, correct result is produced:

<document>
      <document>ss</document>
      <document>sss</document>
      <document>sds</document>
      <document>scs</document>
</document>
1
  • I removed my XSLT 2.0 answer, because the use of tokenize is obviously much more suitable here. XSLT 1.0 good alternative also provided. +1 Jul 26, 2011 at 14:40
2

Use substring-before and substring-after functions, i.e.:

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

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="line">
        <xsl:copy>
            <xsl:value-of select="substring-before(substring-after(substring-after(., ';'), ';'), ';')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Result:

<document>
    <line>ss</line>
    <line>sss</line>
    <line>sds</line>
    <line>scs</line>
</document>
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.