I am new to XSLT, currently I have a json string and I need to derive particular field from the json string . I am currently using fn:parse-json with map:get but the resultant seems to be empty .
Below is the sample snippet:
<xsl:variable name="json-temp" select="fn:parse-json($json-str)" />
<xsl:variable name="result" select="map:get($json-temp,2)" />
When I try to print result (Or) json-temp both seems to be empty - Can someone please help here. and let me know if I am missing anything.
json-str variable has the following value:
{
"testName":"pppp",
"testID":"1234p",
"testDept":"Dept"
}
You should be able to use map:get($json-temp, 'testID') (in XPath 3) or $json-temp?testID in XPath 3.1. I am not sure which value you want to extract with map:get($json-temp,2) as the map has no key of the value 2, that call would only make sense for e.g. map { 2: 'foo' } to extract e.g. foo.
The best way to use JSON in DataPower is by using GatewayScript instead of XSLT. If you really need to stick to XSLT, you can try using:
<xsl:variable name="jsonx" select="dp:stringToJSONx($json-str)"/>
From there, you can extract the field you want by using XPath on the resulting JSONx document.
Related
Following XSLT code works:
<xsl:variable name="ret"> RET </xsl:variable>
<xsl:value-of select="$ret" disable-output-escaping="yes"/>
It outputs "RET". But this code does not work as expected:
<xsl:variable name="ret"> <RET/> </xsl:variable>
<xsl:value-of select="$ret" disable-output-escaping="yes"/>
I expect the output to be <RET/>.
I am doing this for the purpose of capturing output of "xsl:apply-templates" into a variable. The output of "xsl:apply-templates" may include HTML tags (e.g. blah,blah). I hope I can store the output into a variable and do some processing afterworlds.
The xsl:value-of instruction creates a text node. It does so by converting the selection to a string. Your variable contains an empty element that has no string-value - and converting it to a string results in an empty string.
To pass the variable contents to the output tree with no conversion, use the xsl:copy-of instruction.
P.S. In your original question you wrote:
I expect the output to be "<RET/>".
which indicates you thought the variable contains the string "<RET/>" that can be used to create an element by copying it to the output without escaping it. But in order to create such variable you would have to use:
<xsl:variable name="ret"><RET></xsl:variable>
And in any case, that's not a good way to create output.
Use xsl:copy-of instead of xsl:value-of.
I am trying to form an html template with xslt where I need to parse values from a variable containing xml string. But not being able to do so. If I try to print the variable it's showing the whole xml string with < and > being replaced with < and > respectively , but is not being parsed with xpath when tried.
Below is my xml string looks like which I stored in a variable named "SpecificOrderDetails". If I try the expression
<xsl:value-of select="$SpecificOrderDetails/SPECIFICORDERDETAILS/text()"/>
it gives me null value as output
<SPECIFICORDERDETAILS><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Hard-wired Return Path</COMPONENTNAME><COMPONENTID>101401</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Platinum Included FBO Movies</COMPONENTNAME><COMPONENTID>101500</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>iQ Discount</COMPONENTNAME><COMPONENTID>101501</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Primary Discounts</COMPONENTNAME><COMPONENTID>101504</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>03 Foxtel iQ</COMPONENTNAME><COMPONENTID>101601</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>TV - 24 Month Contract</COMPONENTNAME><COMPONENTID>101714</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Standard Install - No Offer</COMPONENTNAME><COMPONENTID>101723</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>My Platinum Eligibility</COMPONENTNAME><COMPONENTID>101900</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Commitment Eligibility</COMPONENTNAME><COMPONENTID>101913</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Platinum Channels</COMPONENTNAME><COMPONENTID>990015</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>4</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Smartcard</COMPONENTNAME><COMPONENTID>101</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>4</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>iQ PDR - Satellite</COMPONENTNAME><COMPONENTID>201</COMPONENTID></ORDERROW></SPECIFICORDERDETAILS>
If I try to print the variable it shows the whole xml string as below
<tr>
<td class="headingSubText" style="font-size:13pt; padding:10px 0px 0px 15px"><SPECIFICORDERDETAILS><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Hard-wired Return Path</COMPONENTNAME><COMPONENTID>101401</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Platinum
Included FBO Movies</COMPONENTNAME><COMPONENTID>101500</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>iQ
Discount</COMPONENTNAME><COMPONENTID>101501</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Primary
Discounts</COMPONENTNAME><COMPONENTID>101504</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>03
Foxtel iQ</COMPONENTNAME><COMPONENTID>101601</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>TV
- 24 Month Contract</COMPONENTNAME><COMPONENTID>101714</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Standard
Install - No Offer</COMPONENTNAME><COMPONENTID>101723</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>My
Platinum Eligibility</COMPONENTNAME><COMPONENTID>101900</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Commitment
Eligibility</COMPONENTNAME><COMPONENTID>101913</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>1</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Platinum
Channels</COMPONENTNAME><COMPONENTID>990015</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>4</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>Smartcard</COMPONENTNAME><COMPONENTID>101</COMPONENTID></ORDERROW><ORDERROW><CATEGORY>4</CATEGORY><ACTION>0</ACTION><COMPONENTNAME>iQ
PDR - Satellite</COMPONENTNAME><COMPONENTID>201</COMPONENTID></ORDERROW></SPECIFICORDERDETAILS>
</td>
</tr>
Please help someone on how to parse the variable inside xslt . Let know if I was not clear enough. This would my first question ever asked on stackoverflow.
Got it working !
Used xmlns:saxon="http://saxon.sf.net/" extension for xslt2.0 and it works perfectly to parse the xml string in the variable like below.
<xsl:value-of select="saxon:parse($SpecificOrderDetails)/SPECIFICORDERDETAILS/ORDERROW[ACTION ='0']"/>
Thanks everyone for helping!
I have a question about extracting a value with xpath
Some part of the xml:
<mark:info>
<mark:Information name="Adress" answer="SomeStreet"/>
<mark:Information name="PhoneNumber" answer="123456789"/>
</mark:info>
I want to receive the text "SomeStreet" with extractValue function in mysql
I was trying to create an xpath expression like
//mark:info/mark:Information[#name='Adress']
but I'm receiving the whole node. I need just the text "SomeStreet" but I'm not sure how to do it.
The xpath //mark:Information[#name="Adress"]/#answer should give SomeStreet
BTW shouldn't your xml have a xmlns specifying what is mark like :
<mark:info xmlns:mark="http://example.com"> ?
I'm new to Xpath and I'm trying to figure out how to extract the attribute of the extension with the value of D000001602 as shown below.
<ClinDoc>
<ComponentOf>
<encompassingEncounter>
<id root="2.16.840.1.113883.3.52.3" extension="D000001602"/>
<effectiveTime>
<low value="20140620135800"/>
<high value="20140701140756"/>
</effectiveTime>
</encompassingEncounter>
</componentOf>
</ClinDoc>
I am using an online extractor with the following code but I can't seem to get it to work:
/clindoc/componentof/encompassingEncounter/id[#root=2.16.840.1.113883.3.529.3]/#extension
//id[#root=2.16.840.1.113883.3.529.3]/#extension
Thanks much!
You're trying to match root with the string value 2.16.840.1.113883.3.52.3, which means you need to represent it as a string literal in the XPath.
Also, node names are case sensitive:
/ClinDoc/Componentof/encompassingEncounter/id[#root="2.16.840.1.113883.3.529.3"]/#extension
I'm using Eclipse to run an XSL 2.0 (XPATH 2.0), and I have the following source:
<testTop>
<Level1 id="abc" Text="from 1-2"/>
<Level1 id="pqr" Text="from 3-44" />
<Level1 id="xyz" Text="from 49-101" />
</testTop>
When I test the following expression in Eclipse, //*[matches(#Text, '\d+-\d+')] I get the right nodes, but not the Text attributes themselves
Level1 ID=abc
Level1 ID=pqr
Level1 ID=xyz
... whereas //#Text gives me the Text attributes. Can anyone help me to understand why?? I'd like to get the Text attribute values and parse them using string functions. THE END RESULT SHOULD LOOK LIKE THIS:
<output originalText="from 1-2" value1="1" value2="2" />
Shouldn't I be getting all the attributes that are part of each node that matched?
Your XPath is selecting the elements that have the attributes you want. If you want to select the #Text that match your pattern, you need to adjust your XPath to select the attributes rather than the element.
You could use this XPath:
//*[matches(#Text, '\d+-\d+')]/#Text
or this XPath:
//*/#Text[matches(., '\d+-\d+')]