troubleshooting Question

XSL: replacing empty space with known value

Avatar of badtz7229
badtz7229Flag for United States of America asked on
CSSXML
11 Comments3 Solutions355 ViewsLast Modified:
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>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros