Link to home
Start Free TrialLog in
Avatar of tesmc
tesmcFlag for United States of America

asked on

XSL: replacing empty space with known value

I have the following xml input

sample 1:
<root>      
      <SeatElementNumber>1</SeatElementNumber>
      <CommandRS>
            <Response>MI&lt;X&gt;TKT NBR&lt;             &gt;</Response>
      </CommandRS>
      <Create>...       </Create>
</root>

sample 2:
<root>      
      <SeatElementNumber>1</SeatElementNumber>
      <CommandRS>
            <Response>MI&lt;X&gt;TKT NBR&lt;1234&gt;</Response>
      </CommandRS>
      <Create>...       </Create>
</root>

      

I currently have code which will extract the values between the lt and gt in order to build a Request xml as follows:
      <CommandRQ><Request><X><1234></Request></CommandRQ>

However, if the the TKT NBR in the input is null (like in sample 1), i want to use the tktNumber i stored in a prior variable. How can i do this in xsl?
This is code I have so far.

    <xsl:key  name="TktMatch"  match="View/Identify/Info[TicketNumber/@Status='TICKETED']/SegmentElementNumber"   use="." />      
    <xsl:variable name="SeatSegment" select="/root/Create/Other/Seat[position() = 1]/SegmentIDRef"/>    
    <xsl:variable name="TktNumber" select="key('TktMatch', $SeatSegment)/@TicketNumber"/>
       
    <xsl:template match="CommandRS">
            <xsl:apply-templates/>
        </CommandRQ>
    </xsl:template>
          
    <xsl:template match="Response">
        <Request>
            <xsl:call-template name="strip-between-tags">
                <xsl:with-param name="str" select="."/>
            </xsl:call-template>
        </Request>
    </xsl:template>

    <xsl:template name="strip-between-tags">
        <xsl:param name="str"/>
        <xsl:choose>
            <xsl:when test="contains($str, '&lt;')">
                <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                <xsl:value-of select="substring-before(substring-after($str, '&lt;'), '&gt;')"></xsl:value-of>
                <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                <xsl:call-template name="strip-between-tags">
                    <xsl:with-param name="str" select="substring-after($str, '&gt;')"/>
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

Gertone,
your code worked (thanks) but when I execute the xsl (using Visual Studios) it seems to be returning the values that are in the <Create> node

The reponse builds as follows

   <CommandRQ><Request><X><1234></Request></CommandRQ>
                  2
                        
                              1
                              82D
                        
                  
                  
                        
                              1
                              2
                              RA
                              
                                    SRT
                                    SEAT
                              
                              
                              Pre-reserved Seating
                              82D
well, that is standard processing,
All I changed was the named template and calling it,

This is a context processing issue that must have been there before
(I could not see full context of XSLT nor source XML)

There are a number of things you can do

If you never need to process the create, add an empty template for it
<xsl:template match="Create"/>

Or process the request from a higher level

instead of

<xsl:template match="Response">
        <Request>
            <xsl:call-template name="strip-between-tags">
                <xsl:with-param name="str" select="."/>
                <xsl:with-param name="deep" select="1"/>
            </xsl:call-template>
        </Request>
    </xsl:template>

do

<xsl:template match="CommandRS">
        <Request>
            <xsl:call-template name="strip-between-tags">
                <xsl:with-param name="str" select="Response"/>
                <xsl:with-param name="deep" select="1"/>
            </xsl:call-template>
        </Request>
    </xsl:template>
Avatar of tesmc

ASKER

I have
      
    <xsl:template match="CommandRS">
        <CommandRQ>
            <xsl:apply-templates/>
        </CommandRQ>
    </xsl:template>
      
    <xsl:template match="Response">
        <Request>
            <xsl:call-template name="strip-between-tags">
                <xsl:with-param name="str" select="."/>
              <xsl:with-param name="deep" select="1"/>
            </xsl:call-template>
        </Request>
    </xsl:template>      
      
      
      
    <xsl:template name="strip-between-tags">
        <xsl:param name="str"/>
        <xsl:param name="deep"/>
            .....
    </xsl:template>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

I do need <Create> in order to get $TktNumber and i processed at a higher level, but it still shows all data from the <Create>
Avatar of tesmc

ASKER

<xsl:template match="Create"/>
<xsl:template match="SeatElementNumber"/>

that worked.
but why is it necessary to include that, i dont' understand?
Please send me the full XSLT you have and a full XML source
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tesmc

ASKER

Thanks for ur help and explanation