XML value contains xml characters - xslt

Actually an XML is passed as a value of Input XML attribute. Please help to fetch the XML value using XSLT 1.0
<Root>
<Element1 ProductDetails="<Input xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Amount="15632" Product="Pencil"></Input>"/>
</Root>
I need to just fetch the value of ProductDetails attribute.
i.e. only the below part using XSLT 1.0
<Input xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Amount="15632" Product="Pencil"></Input>

check it:-
<xsl:value-of select="#ProductDetails"/>

Related

Difficulty in Accessing Attribute value (Xpath) XSLT

I could not able to access PartNumber/Description value under attribute tag with below xpath but and soap xml is given below.Let me know any further details required.
Please help on this.Thanks inadvance!.
Xpath being used And SOAP XML Message:
<xsl:value-of select="/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']/#*[local-name()='PartNumber']"/>
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header />
<s:Body>
<PullCustomerPartsPricingResponse xmlns="http://cdx.dealerbuilt.com/Api/0.99/">
<PullCustomerPartsPricingResult xmlns:a="http://schemas.datacontract.org/2004/07/DealerBuilt.BaseApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:CustomerPart>
<a:Placement>
<a:GroupId>10</a:GroupId>
</a:Placement>
<a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models.Parts">
<b:AddedDate>2017-12-19T00:00:00</b:AddedDate>
<b:DealerPartId>287925</b:DealerPartId>
<b:Description>BAT (51/500AMP85)</b:Description>
<b:PartNumber>31500SB2yy1M</b:PartNumber>
<b:QuantityLostMonthToDate>0</b:QuantityLostMonthToDate>
</a:Attributes>
<a:PartKey>GZ287925</a:PartKey>
<a:CustomerListPrice xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models">
<b:Amount>130.49</b:Amount>
<b:Currency>UsDollar</b:Currency>
</a:CustomerListPrice>
</a:CustomerPart>
</PullCustomerPartsPricingResult>
</PullCustomerPartsPricingResponse>
</s:Body>
</s:Envelope>
Regards
Vardhan
You have ended your long xpath expression with this..
/#*[local-name()='PartNumber']
But PartNumber is not an attribute. It is an element named PartNumber that is a child of an element that happened to be named Attributes, but that does not actually make it an attribute in XML terms!
It should look like this...
<xsl:value-of select="/*[local-name()='Envelope']
/*[local-name()='Body']
/*[local-name()='PullCustomerPartsPricingResponse']
/*[local-name()='PullCustomerPartsPricingResult']
/*[local-name()='CustomerPart']
/*[local-name()='Attributes']
/*[local-name()='PartNumber']"/>
Although it would be really better if you declared the namespaces in your XSLT and used then in your xpath.
NameSpaces from the request XML need to be used in xslt as well.Following xsl gave the PartNumber value.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:a="http://schemas.datacontract.org/2004/07/DealerBuilt.BaseApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.datacontract.org/2004/07/DealerBuilt.Models.Parts">
<xsl:output method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:value-of select="*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='PullCustomerPartsPricingResponse']/*[local-name()='PullCustomerPartsPricingResult']/*[local-name()='CustomerPart']/*[local-name()='Attributes']/*[local-name()='PartNumber']"/>
</xsl:template>
</xsl:stylesheet>

How to convert soap response containing CDATA to new formatted xml using xslt?

I want to convert below code to some formatted xml code,
Input For XSLT Transformation:
<soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<rejectQuoteXMLResponse
xmlns='http://xxx.group.com'>
<out>
<![CDATA[
<TFGCPLResultXMLDO>
<processInstanceName>reejectQuoteXML</processInstanceName>
<duration>0</duration>
<accumulatedNumberOfExceptions>0</accumulatedNumberOfExceptions>
<accumulatedNumberOfErrors>0</accumulatedNumberOfErrors>
<accumulatedNumberOfWarnings>0</accumulatedNumberOfWarnings>
<numOfExceptions>0</numOfExceptions>
<numOfErrors>0</numOfErrors>
<numOfWarnings>0</numOfWarnings>
<BusinessMessages>
<BusinessErrors/>
<BusinessWarnings/>
<BusinessGenericMessages/>
</BusinessMessages>
<requestedTransSuccessfulInd>true</requestedTransSuccessfulInd>
<modifySuccessfulInd>false</modifySuccessfulInd>
<copySuccessfulInd>false</copySuccessfulInd>
<responseXMLString>
<RejectPolicyRes>
<status>Success</status>
<message>Reject Policy successful</message>
</RejectPolicyRes>
</responseXMLString>
</TFGCPLResultXMLDO>
]]>
</out>
</rejectQuoteXMLResponse>
</soap:Body>
</soap:Envelope>
Required Output:
<QuoteRejectRs>
<UserId>344758</UserId>
<QuoteDetails>
<QuoteNumber>PA-Q450000</QuoteNumber>
<Status>Success</Status>
<Message>Reject Policy successful</Message>
</QuoteDetails>
</QuoteRejectRs>
XSLT Transformation code:
<!-- <xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:md1="http://http://xxx.group.com" xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xs md1"
extension-element-prefixes="exsl"
version="2.0"> -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<!--XML Response for Quote Reject which tell request has been submitted with success or fail -->
<QuoteRejectRs>
<UserId></UserId>
<QuoteDetails>
<QuoteNumber></QuoteNumber>
<Status>
<xsl:value-of select="substring-before(substring-after(.,'Envelope/Body/TFGCPLResultXMLDO>'), '<Envelope/Body/TFGCPLResultXMLDO/responseXMLString/RejectPolicyRes/status')"/>
</Status>
<Message>
<xsl:value-of select="substring-before(substring-after(.,'Envelope/Body/TFGCPLResultXMLDO>'), '<Envelope/Body/TFGCPLResultXMLDO/responseXMLString/RejectPolicyRes/message')"/>
</Message>
<!-- <Status><xsl:value-of select="soap:Envelope/soap:Body/md1:rejectQuoteXMLResponse/md1:out/md1:status"/></Status><Message><xsl:value-of select="Envelope/Body/TFGCPLResultXMLDO/responseXMLString/RejectPolicyRes/message"/></Message>-->
</QuoteDetails>
</QuoteRejectRs>
</xsl:template>
</xsl:stylesheet>
I have tried and able to convert xml which contains namespace (soap:xxx) , but unable to convert xml which contains CDATA.
I have tried but getting response containing < > format.
So anyone know solution for such task.
You complain that you are
getting response containing < > format.
That's easy enough to fix. xsl:value-of has an attribute disable-output-escaping specifically for that purpose, so something like
<Status>
<xsl:value-of
select="substring-before(substring-after(.,'Envelope/Body/TFGCPLResultXMLDO>'), '<Envelope/Body/TFGCPLResultXMLDO/responseXMLString/RejectPolicyRes/status')"
disable-output-escaping="yes"/>
</Status>
<Message>
<xsl:value-of
select="substring-before(substring-after(.,'Envelope/Body/TFGCPLResultXMLDO>'), '<Envelope/Body/TFGCPLResultXMLDO/responseXMLString/RejectPolicyRes/message')"
disable-output-escaping="yes"/>
</Message>
ought to do the job you seem to be looking for.
HOWEVER, I urge you to take a different approach. Picking apart and parsing XML text via string functions is bad news. What you really should do is parse the embedded XML as an XML document, and transform that. Unfortunately, XSLT 1.0 and 2.0 do not have a standard mechanism for that, but some implementations add that as an extension, and depending on how you are performing the transformation, it may be comparatively easy to install your own extension function for the purpose. Alternatively, you could extract the embedded XML as a preliminary step, and then transform just that.

How to encode(escaping special characters) in XSLT1.0 using Apache Software Foundation (Xalan XSLTC) Processor

I am using XSLT 1.0, Apache Software Foundation (Xalan XSLTC) processor. I am not supposed to use XSLT 2.0. I have the following xml in which the value of <prvNum> has some special characters.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<prvNum>SPECIAL#1&</prvNum>
</root>
Now I want to perform percent-encoding for the value of <prvNum>. For example the value should be changed as below after percent encoding:
SPECIAL%231%26
My output xml should looklike below:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<prvNum>SPECIAL%231%26</prvNum>
</root>
Can anybody tell me how do that?

How to modify xml declaration in XSLT

I want to modify processing instructions in a source xml with XSLT, for example:
XML INPUT
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
XML OUTPUT
<?xml version="1.0" encoding="WINDOWS-1252"?>
<root>
</root>
Can I do this with XSLT?
thanks in advance.
That is the XML declaration, it is not a processing instruction. If you want a particular output encoding then use e.g. <xsl:output encoding="Windows-1252"/>. But any XML parser is required to support UTF-8 so using an 8-bit code page in the age of Unicode and XML does not improve interoperability.

Passing and parsing XML as a parameter to XSLT 2.0

I'm trying to figure out how to pass XML into a XSLT document and parse it as you would on any node.
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="xmlparam"></xsl:param>
<xsl:template match="/">
<xsl:value-of select="node()"></xsl:value-of>
<xsl:value-of select="$xmlparam/coll"></xsl:value-of>
</xsl:template>
</xsl:stylesheet>
xmlparam:
<?xml version="1.0" encoding="UTF-8"?>
<coll>
<title>Test</title>
<text>Some text</text>
</coll>
Input:
<?xml version="1.0" encoding="UTF-8"?>
<coll>
Root doc
</coll>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Root doc
XPTY0019: Required item type of first operand of '/' is node(); supplied value has item type xs:untypedAtomic
Does anyone know how to parse XML passed in as a parameter to XSLT? Due to certain restraints, I cannot read in a file, it needs to be a parameter.
You could get your XML input to be parsed by the style sheet by using the document function, e.g. like (from memory, so maybe not completely accurate)
<xsl:variable name="myData" select="document('myData')"/>
AND registering a custom URIResolver with the with your Transformer. This custom URIResolver will get "myData" as value of the parameter href of its resolve method and could then obtain the content of the document from e.g. a String. This would give you roughly the same flexibility as adding the content as a parameter.
Code sketch (assuming the obvious implementation of MyURIResolver):
Transformer myTransformer = getMyTransformer();
String myXmlDocument = getMyXmlDocumetAsString();
URIResolver myURIResolver = new MyURIResolver();
myURIResolver.put("myData", myXmlDocument);
myTransformer.setURIResolver(myURIResolver);
myTransformer.transform(source, result);