Avatar of badtz7229
badtz7229Flag 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>
CSSXML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
badtz7229

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
Gertone (Geert Bormans)

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>
ASKER
badtz7229

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>
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
badtz7229

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>
ASKER
badtz7229

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

that worked.
but why is it necessary to include that, i dont' understand?
Gertone (Geert Bormans)

Please send me the full XSLT you have and a full XML source
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
badtz7229

Thanks for ur help and explanation
Gertone (Geert Bormans)

welcome