VishalP
VishalP

Reputation: 13

Facing difficulty writing xslt for XML

This first time I am dealing with xslt creations, finding it really tricky.

I have request XML and using xslt it is supposed to generate XML with below mentioned o/p.

Utility performing this operation is already built in C#. I need to write XSLT that will correctly read values from XML and create new XML with expected format.

I am trying to write XSLT to transform XML(i/p) :

    <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" 
    xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Body>
        <ExecuteESI xmlns="http://TTTT.com/Enterprise/ServiceGateways/">
            <context xmlns:b="http://TTTT.com/Enterprise/ServiceGateways/Core/Contracts/" 
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <request xmlns:b="http://TTTT.com/Enterprise/ServiceGateways/ExternalService/Request/" 
                    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                    <b:Message>
                        <ns2:ReqProv xmlns:ns2="http://TTTT.com/TTTT/TTTTestJSONESIRequest/">
                            <ns2:CID>TTTT</ns2:CID> 
                            <ns2:TIME>18732081720160855</ns2:TIME> 
                            <ns2:HASH>40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d5452da0</ns2:HASH> 
                        </ns2:ReqProv>
                    </b:Message>
                </request>
            </context>
        </ExecuteESI>
    </s:Body>
</s:Envelope>

to Below mentioned format (o/p):

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">CID=TTTT&amp;TIME=18732081720160855&amp;HASH=40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d545</string>

I have written following XSLT, which doesn't work

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:ns="http://asurion.com/TTTT/TTTTTestJSONESIRequest/"
                xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
                exclude-result-prefixes="msxsl xsl ns">
  <xsl:output method="xml" />
  <xsl:template match="/">

    <xsl:variable name="CID">
      <xsl:value-of select="./ns:ReqProv/ns:CID"/>
    </xsl:variable>
    <xsl:variable name="TIME">
      <xsl:value-of select="./ns:ReqProv/ns:TIME"/>
    </xsl:variable>
    <xsl:variable name="HASH">
      <xsl:value-of select="./ns:ReqProv/ns:HASH"/>
    </xsl:variable>

    CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" />

  </xsl:template>

</xsl:stylesheet>

I have tried different combinations of <xsl:value-of... and <xsl:template match... and <xsl:template name.... but nothings seems to work.

Upvotes: 1

Views: 136

Answers (3)

michael.hor257k
michael.hor257k

Reputation: 116959

There are numerous problems with your attempt - the most serious one is using the wrong namespace. Try instead:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <string>
        <xsl:text>CID=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:CID"/>

        <xsl:text>@amp;TIME=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:TIME"/>

        <xsl:text>@amp;HASH=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:HASH"/>
    </string>
</xsl:template>

</xsl:stylesheet>

Note:

  • there is very little point in defining a variable for something you only need once;

  • it is much more convenient to use the xsl:text instruction when outputting literal text; it also makes the code easier to read and maintain.

Upvotes: 2

LarsH
LarsH

Reputation: 27996

You're almost there. You need to supply the <string> result element in the stylesheet template, fix your XPath expressions, and fix a mismatched namespace URI.

You'll want to change your template that matches "/" to something like the following:

  <xsl:template match="/">
    <!-- No need to use value-of here.
     Also use .// instead of ./, because ns:ReqProv is not a child of /. -->
    <xsl:variable name="CID" select=".//ns:ReqProv/ns:CID"/>
    <xsl:variable name="TIME" select=".//ns:ReqProv/ns:TIME"/>
    <xsl:variable name="HASH" select=".//ns:ReqProv/ns:HASH"/>

    <string>CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></string>

  </xsl:template>

You also need to change one of these two namespace URIs so that they match, in the input XML and the XSLT:

"http://TTTT.com/TTTT/TTTTestJSONESIRequest/"

and

"http://asurion.com/TTTT/TTTTTestJSONESIRequest/"

I'm guessing these are really the same in your actual input XML and XSLT, but you changed one of them in posting the question here. So you'll want to make sure that they match.

Upvotes: 1

Rudramuni TP
Rudramuni TP

Reputation: 1278

Try this:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:ns="http://asurion.com/TTTT/TTTTTestJSONESIRequest/"
            xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
            xmlns:s="http://www.w3.org/2003/05/soap-envelope"
            xmlns:ns2="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
            exclude-result-prefixes="msxsl xsl ns">
<xsl:output method="xml" />
<xsl:template match="/">

<xsl:variable name="CID">
  <xsl:value-of select="//ns2:ReqProv/ns2:CID"/>
</xsl:variable>
<xsl:variable name="TIME">
  <xsl:value-of select="//ns2:ReqProv/ns2:TIME"/>
</xsl:variable>
<xsl:variable name="HASH">
  <xsl:value-of select="//ns2:ReqProv/ns2:HASH"/>
</xsl:variable>

<xsl:element name="string">CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></xsl:element>

</xsl:template>

</xsl:stylesheet>

Needs to change

<xsl:value-of select="./ns:ReqProv/ns:CID"/>

to

<xsl:value-of select="//ns2:ReqProv/ns2:CID"/>

Because, in put xml ns2 is the namespace for the element CID.

OutPut:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">CID=TTTT&amp;TIME=18732081720160855&amp;HASH=40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d5452da0</string>

Upvotes: 1

Related Questions