tesmc
asked on
XSL: replacing empty space with known value
I have the following xml input
sample 1:
<root>
<SeatElementNumber>1</Seat ElementNum ber>
<CommandRS>
<Response>MI<X>TKT NBR< ></Response>
</CommandRS>
<Create>... </Create>
</root>
sample 2:
<root>
<SeatElementNumber>1</Seat ElementNum ber>
<CommandRS>
<Response>MI<X>TKT NBR<1234></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><12 34></Reque st></Comma ndRQ>
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[ TicketNumb er/@Status ='TICKETED ']/Segment ElementNum ber" use="." />
<xsl:variable name="SeatSegment" select="/root/Create/Other /Seat[posi tion() = 1]/SegmentIDRef"/>
<xsl:variable name="TktNumber" select="key('TktMatch', $SeatSegment)/@TicketNumbe r"/>
<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, '<')">
<xsl:text disable-output-escaping="y es"><</ xsl:text>
<xsl:value-of select="substring-before(s ubstring-a fter($str, '<'), '>')"></xsl:value-of>
<xsl:text disable-output-escaping="y es">></ xsl:text>
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="substring-after($s tr, '>')"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
sample 1:
<root>
<SeatElementNumber>1</Seat
<CommandRS>
<Response>MI<X>TKT NBR< ></Response>
</CommandRS>
<Create>... </Create>
</root>
sample 2:
<root>
<SeatElementNumber>1</Seat
<CommandRS>
<Response>MI<X>TKT NBR<1234></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><12
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[
<xsl:variable name="SeatSegment" select="/root/Create/Other
<xsl:variable name="TktNumber" select="key('TktMatch', $SeatSegment)/@TicketNumbe
<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, '<')">
<xsl:text disable-output-escaping="y
<xsl:value-of select="substring-before(s
<xsl:text disable-output-escaping="y
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="substring-after($s
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
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
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>
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
ASKER
<xsl:template match="Create"/>
<xsl:template match="SeatElementNumber"/ >
that worked.
but why is it necessary to include that, i dont' understand?
<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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for ur help and explanation
welcome
ASKER
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><12
2
1
82D
1
2
RA
SRT
SEAT
Pre-reserved Seating
82D