1
MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3

above string pattern I would like to transform into below XML format

<filters>
<element group="MSH">
  <location path="MSH.1"/>
  <location path="MSH.2"/>
  <location path="MSH.3"/>
</element>
<element group="PID">
  <location path="PID.1"/>
  <location path="PID.2"/>
</element>
<element group="ORC">
  <location path="ORC.1"/>
  <location path-"ORC.3"/>
</elment>
</filters>

Can I have sample XSLT to achieve above scenario

Note : I am making use of XSLT 2.0 ans saxon transformer

Laxmikanth Samudrala
  • 2,203
  • 5
  • 28
  • 45

3 Answers3

3

Assuming you start the transformation in Saxon with parameters

-it:main in=MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3

try

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

<xsl:param name="in"/>

<xsl:template name="main">
<filters>
  <xsl:variable name="tokens" select="tokenize($in, ':')"/>
  <xsl:for-each-group select="$tokens" group-by="substring-before(., '.')"/>
   <element group="{current-grouping-key()}">
     <xsl:for-each select="current-group()">
       <location path="{.}"/>
     </xsl:for-each>
   </element>
  </xsl:for-each-group>
</filters>
<xsl:template>

</xsl:stylesheet>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

You can use the unparsed-text() function in XSLT 2.0 to load a document as a string variable.

You would then probably use the xsl:analyze-string instruction to split the string, first on the ":" character, then possibly on the period character.

samjudson
  • 56,243
  • 7
  • 59
  • 69
0

XSLT is meant to manipulate XML (into something else). So you will have to XMLize your string first, at minimum

<xmlize>MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3</xmlize>

You can then have an XSLT matching on the root element and you can use `tokenize()' to process your string. See this discussion

Doing this with XSLT is a bit weird; can't you use another script/language that has String manipulation functions with regular expressions splitting ?

nb: when you "xmlize", do not forget to escape '<' '>' and '&' if appropriate

Community
  • 1
  • 1
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • I agree with @BGR: are you sure XSLT is the right tool for this? It is good for outputting well-formed XML, but not that convenient for parsing non-XML text. – LarsH Jul 23 '12 at 15:30
  • 3
    XSLT 2.0 is ideally suited to this task. See my response. – Michael Kay Jul 23 '12 at 20:37
  • @Michael Kay. I respect the fact that your are the XSLT Herald, but "doable in XSLT 2.0": yes - "ideally suited": no. Linking a "fat" 3rd party library, not used for its primary purpose (XML Transformation), for something that can be done extremely efficiently with simple String manipulation in 5 lines of Java 101 code? Academic interest aside, I certainly hope none of my developers ever come up with this. – Bruno Grieder Jul 24 '12 at 08:16
  • (a) there's a lot less fat in XSLT than in Java. (b) This kind of use case is very much within the scope of what XSLT 2.0 was designed to do. (c) A 5-line Java solution almost certainly doesn't handle all the edge cases of XML serialization correctly. You need to revise your views on XSLT, it is much more powerful than you think. And if you're using Java for things that can be done in XSLT, then you are wasting your company's time and money. – Michael Kay Jul 25 '12 at 02:32
  • @MichaelKay !?. Fortunately, you are not the one deciding how to waste my company's time and money, I am. Regards. – Bruno Grieder Jul 25 '12 at 06:32