?
Solved

xsl parsing: ABC1;;VAL1|ABC2;;VAL2|.....|ABCN;;VALN into specific format

Posted on 2007-08-06
9
Medium Priority
?
420 Views
Last Modified: 2013-11-18
i need to parse the following string: ABC1;;VAL1|ABC2;;VAL2|.....|ABCN;;VALN using xsl into

<ABC>
    <XYZ>ABC1</XYZ>
</ABC>

<MNP>
      VAL1>
</MNP>

.....

How can i do that using xsl? Thanks.
0
Comment
Question by:jgordin
  • 5
  • 4
9 Comments
 

Author Comment

by:jgordin
ID: 19638205
correction:

<VAL1>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 19638342
I am not quite certain that I understood your logic completely,
but here is a first attempt

note that in order to process a string this way in XSLT1,
you need to walk through it recursively
The XSLT2 solution would be much nicer, if you could use XSLT2

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   
    <xsl:output indent="yes"/>
   
    <xsl:template match="test">
        <xsl:call-template name="processStr">
            <xsl:with-param name="str" select="." />
        </xsl:call-template>
    </xsl:template>
   
    <xsl:template name="processStr">
        <xsl:param name="str" />
        <xsl:choose>
            <xsl:when test="contains($str, '|')">
                <xsl:call-template name="processSubStr">
                    <xsl:with-param name="str" select="substring-before($str, '|')"></xsl:with-param>
                </xsl:call-template>
                <xsl:call-template name="processStr">
                    <xsl:with-param name="str" select="substring-after($str, '|')"></xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="processSubStr">
                    <xsl:with-param name="str" select="$str"></xsl:with-param>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
       
       
    </xsl:template>
   
    <xsl:template name="processSubStr">
        <xsl:param name="str"/>
        <xsl:if test="contains($str, ';;')">
            <ABC>
                <XYZ><xsl:value-of select="substring-before($str, ';;')"/></XYZ>
            </ABC>
            <MNP>
                <xsl:element name="{substring-after($str, ';;')}"></xsl:element>
            </MNP>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

note that I wrapped the string in a <test> element

You could change the behaviour in the named template processSubString

cheers

geert
0
 

Author Comment

by:jgordin
ID: 19639532
when i run this code i get an error : Name cannot begin with the '1' character, hexadecimal value of 0x31

any idea how to fix it?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jgordin
ID: 19639704
code that i run:


<xsl:template name="callProcessStr">
  <xsl:param name="str"/>
  <xsl:choose>

    <xsl:when test="contains($str, '|')">

      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="substring-before($str, '|')"></xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="substring-after($str, '|')"></xsl:with-param>
      </xsl:call-template>

    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="$str"></xsl:with-param>
      </xsl:call-template>
    </xsl:otherwise>

  </xsl:choose>
  </xsl:template>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 19639735
in your string you have values like this
ABC1;;VAL1
and you want VAL1 to be the name of an element
This requires that VAL1 is always a valid XML name,
which cannot start with a number or have a space in there

it would be better to have something like this
          <MNP>
                <item name="{substring-after($str, ';;')}" />
            </MNP>

instead,
if that would be acceptable XML

or prepend an alfabetical character and strip out the spaces

          <MNP>
                <xsl:element name="{concat('T', translate(normalize-space(substring-after($str, ';;')), ' ', ''))}"></xsl:element>
            </MNP>

cheers

Geert
0
 

Author Comment

by:jgordin
ID: 19639982
thanks. i ran the following code:

<xsl:template name="callProcessStr">
  <xsl:param name="str"/>
  <xsl:choose>

    <xsl:when test="contains($str, '|')">

      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="substring-before($str, '|')"></xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="substring-after($str, '|')"></xsl:with-param>
      </xsl:call-template>

    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="$str"></xsl:with-param>
      </xsl:call-template>
    </xsl:otherwise>

  </xsl:choose>
  </xsl:template>



  <xsl:template name="processSubStr">
  <xsl:param name="str"/>
  <xsl:if test="contains($str, ';;')">
   
    <ABC>
      <XYZ>
        <xsl:value-of select="substring-before($str, ';;')"/>
      </XYZ>
    </ABC>
   
    <MNP>
     
      <xsl:value-of select= "substring-after($str, ';;')"/>
    </MNP>
 
  </xsl:if>
</xsl:template>


i had the following string: ABC1;;VAL1|ABC2;;VAL2|ABC3;;VAL3. the code parsed correctly only first part:

<ABC>
   <XYZ>ABC1</XYZ>
</ABC>
<MNP>
   VAL1
</MNP>

but not the rest except for ABC2:
<ABC>
  <XYZ> ABC2</XYZ>
</ABC>
<MNP>
  VAL2|ABC3;;VAL3
</MNP>

 VAL2|ABC3;;VAL3 is not parsed correctly. how does one fix that?


0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 19640023
you stop the recursion by calling processSubstring instead of processString
for the part after the '|'

the first named template should be this (look at the original answer)

<xsl:template name="callProcessStr">
  <xsl:param name="str"/>
  <xsl:choose>

    <xsl:when test="contains($str, '|')">

      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="substring-before($str, '|')"></xsl:with-param>
      </xsl:call-template>

      <xsl:call-template name="callProcessStr"> <!-- call different template here -->
        <xsl:with-param name="str" select="substring-after($str, '|')"></xsl:with-param>
      </xsl:call-template>

    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="processSubStr">
        <xsl:with-param name="str" select="$str"></xsl:with-param>
      </xsl:call-template>
    </xsl:otherwise>

  </xsl:choose>
  </xsl:template>

0
 

Author Comment

by:jgordin
ID: 19640210
it works. thank you for your help.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 19640455
welcome
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question