XSLT - using hardcoded values and looping on them - xslt

I'm very new to xslt and trying to learn. The focus to transform my data xml and bind the output xml to the adobe form.
I have a xml of the following structure.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<envelope xmlns="http://www.mydata.de/xem/reporting/datafile">
<sources>
<source>
<data>
<REPORT>
<EM_ESOURCE>
<EM_ES_MEASURE>
<ID>1037343</ID>
<ES_ID>1006222</ES_ID>
<ES_NAME>MFC-D-002</ES_NAME>
<EM_MAT_NAME>Cyprinella leedsi</EM_MAT_NAME>
<START_DATE>1/19/98</START_DATE>
<LABORATORY>Thornton</LABORATORY>
<LC50>>100%</LC50>
<TESTTYPE/>
<IC25/>
</EM_ES_MEASURE>
<EM_ES_MEASURE>
<ID>1037344</ID>
<ES_ID>1006222</ES_ID>
<ES_NAME>MFC-D-002</ES_NAME>
<EM_MAT_NAME>C Dubia</EM_MAT_NAME>
<START_DATE>3/2/98</START_DATE>
<LABORATORY>Thornton</LABORATORY>
<LC50>>120%</LC50>
<TESTTYPE>Routine</TESTTYPE>
<IC25/>
</EM_ES_MEASURE>
</EM_ESOURCE>
</REPORT>
</data>
</source>
</sources>
This is the result of one of the queries. The Adobe report requires the materials to be shown always in a particular order regardless of what the query returns. So I decided to hardcode the order of materials in the xsl internally, loop on this list and then fetch corresponding "LC50" values from the data xml.
Following is the xsl that I started:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rep="http://www.technidata.de/xem/reporting/datafile"
xmlns:s="http:/materials.data"
xmlns:java="http://xml.apache.org/xslt/java">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8"/>
<!-- Suppress copy of restrictions -->
<!--xsl:template match="text()|#*"/-->
<xsl:key name="material-lookup" match="s:material" use="s:name"/>
<xsl:template match="/">
<REPORT>
<HEADERS>
<STARTDATE><xsl:value-of select="substring(/rep:envelope/rep:sources/rep:restrictions/rep:START_DATE,1,10)"/></STARTDATE>
<ENDDATE><xsl:value-of select="substring(/rep:envelope/rep:sources/rep:restrictions/rep:START_DATE,16,24)"/></ENDDATE>
<FACILITY><xsl:value-of select="normalize-space(/rep:envelope/rep:sources/rep:restrictions/rep:ID_ATTRIBUTE_ID_NAME)"/></FACILITY>
</HEADERS>
<MATERIALS>
</MATERIALS>
</REPORT></xsl:template>
<s:materials>
<s:material>
<s:name>Cyprinella leedsi</s:name>
<s:parameter>LC(ROUTINE) </s:parameter>
</s:material>
<s:material>
<s:name>C Dubia</s:name>
<s:parameter>LC50(ROUTINE) </s:parameter>
</s:material>
</s:materials>
</xsl:stylesheet>
I'm not sure how to fill in the the MATERIALS node with the materials in the order defined in the xslt and their corresponding LC50 values from the data xml.

Use the document('') function to XPath into the XSLT file itself.

Related

Combining input XML to produce another XML output

Trying to build a POC that does the following:
Given a short input XML, take the values from it and insert them into a larger XML file of a known format.
So if this was my input XML:
<root>
<transaction ID="TX123" source-system="xyz" timestamp="2015-10-15T14:20:35.954Z" dest-system="abc" status="success" applicationID="some_app" originator="MQ">
</transaction>
</root>
And i'd have to take these values and insert them into this: MQ FTE Transfer Log message format
I'd have to insert the values in the transferSet node (timestamp) and metaDataSet.
What would the XSLT have to look like in order to get this done?
Thanks a bunch in advance!
Slava.
Your XLS should look like this:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<transaction>
<!-- more elements... -->
<xsl:apply-templates select="transaction"/>
</transaction>
</xsl:template>
<xsl:template match="transaction">
<transferSet>
<xsl:attribute name="startTime"><xsl:value-of select="#timestamp" /></xsl:attribute>
<metaDataSet>
<metaData key="com.ibm.wmqfte.SourceAgent"><xsl:value-of select="#source-system"/></metaData>
<!-- more elements... -->
</metaDataSet>
</transferSet>
</xsl:template>
With your source input, it should generate an XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<transaction>
<transferSet startTime="2015-10-15T14:20:35.954Z">
<metaDataSet>
<metaData key="com.ibm.wmqfte.SourceAgent">xyz</metaData>
</metaDataSet>
</transferSet>
</transaction>

Format-number from ###.0 to ###.00

I working with a project in BizTalk where use xslt to convert from and edifact file to an UBL file.
The edifact file contains price values of ###.0 and that do not work. I want to change it to ###.00 using format-number. But I cannot make it work.
This is what I have made so far:
<cbc:Value>
<xsl:variable name="SumOfNodes" select="edi:PRI/edi:C509/C50902"/>
<xsl:value-of select="format-number($SumOfNodes, '0.00')"/>
</cbc:Value>
I am using this stylesheet:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:edi="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/MEDIAMARKT"
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Order-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
exclude-result-prefixes="msxsl edi">
Any ideas on how to solve this??
This may be due to undeclared namespace in your XSLT. Please check whether namespaces are declared correctly in your XSLT. Unless you show the full XSLT coding with XML coding, we cannot able to provide solution. However please refer the below Sample XML and XSLT with the output
XSLT:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="chapter">
<Value>
<xsl:variable name="num" select="number"/>
<xsl:value-of select="format-number($num,'00.00')"/>
</Value>
</xsl:template>
</xsl:stylesheet>
Sample XML
<?xml version="1.0"?>
<chapter>
<number>45</number>
</chapter>
Output
<?xml version='1.0' ?>
<Value>45.00</Value>

How do I extract a field from this XML using XSLT 1.0?

I'm an XSLT newbie, and need to use XSLT to extract some fields from a trademark file from the US Patent and Trademark Office. Here's a very simplified copy of a typical file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Transaction xmlns:ns2="urn:us:gov:doc:uspto:trademark:status" xmlns="http://www.wipo.int/standards/XMLSchema/trademarks">
<TradeMarkTransactionBody>
<TransactionContentDetails>
<TransactionCode>National Trademark Information</TransactionCode>
<TransactionData>
<TradeMarkDetails>
<TradeMark>
<RegistrationOfficeCode>US</RegistrationOfficeCode>
<ApplicationNumber>74631225</ApplicationNumber>
<ApplicationDate>1995-02-07-05:00</ApplicationDate>
<RegistrationNumber>2178784</RegistrationNumber>
<RegistrationDate>1998-08-04-04:00</RegistrationDate>
<FilingPlace>US</FilingPlace>
<MarkCurrentStatusDate>2008-08-11-04:00</MarkCurrentStatusDate>
<WordMarkSpecification>
<MarkVerbalElementText>JAVA </MarkVerbalElementText>
</WordMarkSpecification>
</TradeMark>
</TradeMarkDetails>
</TransactionData>
</TransactionContentDetails>
</TradeMarkTransactionBody>
</Transaction>
I would like to be able to produce:
App number: 74631225
Here are a couple of my failed attempts; Attempt #1:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
App number: <xsl:value-of select="/Transaction/TradeMarkTransactionBody/TransactionContentDetails/TransactionData/TradeMarkDetails/TradeMark/ApplicationNumber"/>
</xsl:template>
</xsl:stylesheet>
Produces only:
App number:
Attempt #2:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/Transaction/TradeMarkTransactionBody/TransactionContentDetails/TransactionData/TradeMarkDetails/TradeMark">
App number: <xsl:value-of select="ApplicationNumber"/>
</xsl:template>
</xsl:stylesheet>
produces:
National Trademark Information
US
74631225
1995-02-07-05:00
2178784
1998-08-04-04:00
US
2008-08-11-04:00
JAVA
Any help would be appreciated. Once I get past this gate and have at least one field working, I hope I can get into the real substance of my project. If it matters, I'm using both MSXSL and Treebeard (which uses Saxon, I think) for my testing.
Your XSLT code is missing the namespace declaration. Check out the root element in your Xml document, it says this:
xmlns="http://www.wipo.int/standards/XMLSchema/trademarks"
That means, any of the elements in your Xml document are in that namespace.
In the XSLTs, on the other hand, you did not specify any namespace, which means that your XSLT processor looks for element names specified in the XSLT with the "blank namespace" - so e.g. Transaction mentioned in your XSLT is not the same element as Transaction (from the http://www.wipo.int/standards/XMLSchema/trademarks namespace) mentioned in your Xml document.
XSLT, or rather XPath, does not know the concept of a "default" (prefix-less) namespace, which is why you will have to assign some arbitrary prefix - say tm:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tm="http://www.wipo.int/standards/XMLSchema/trademarks">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
App number: <xsl:value-of select="/tm:Transaction/tm:TradeMarkTransactionBody/tm:TransactionContentDetails/tm:TransactionData/tm:TradeMarkDetails/tm:TradeMark/tm:ApplicationNumber"/>
</xsl:template>
</xsl:stylesheet>
This should get you a step closer to what you are looking for. I can try this only in a few hours from now; if you need further assistance, please leave a comment and I'll check back on this question.

Display current date in XML element

I'm using an XSL to transform one XML into another. My problem is that in one element I have to display the current date with the format:YYYYMMDD.
I tried using a variable like these:
<xsl:variable name="dateNow" select="current-dateTime()"/>
<xsl:variable name="dateNow2" select="current-date()"/>
And then tried to format then, but no success.
<FRUEHESTER_LIEFERTERMIN><xsl:value-of select="format-dateTime($dateNow, '[Y0001][M01][D01]')"/></FRUEHESTER_LIEFERTERMIN>
What exactly is happening (what does "no success" mean). What XSLT processor are you using?
Here is a minimal test case of what you are trying to do (input XML document doesn't matter)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="dateNow" select="current-dateTime()"/>
<xsl:variable name="dateNow2" select="current-date()"/>
<xsl:template match="/">
<FL><xsl:value-of select="format-dateTime($dateNow, '[Y0001][M01][D01]')"/></FL>
</xsl:template>
</xsl:stylesheet>
and here is what it produces -- do you get the same if you try this test case?
<?xml version="1.0" encoding="UTF-8"?>
<FL>20120111</FL>
If you want to show date in the following format Wed, 27-Oct
Use following code in XML
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="EE, dd-MMM"
android:textColor="#color/gray_100"
android:textSize="15sp"
android:layout_gravity="center"
android:padding="5dp"
android:textStyle="bold" />
If you want to show time like 09:08
Use the following code in XML
<TextClock
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:format12Hour="hh:mm"
android:textColor="#color/gray_100"
android:textSize="60sp" />
if you want to show time with AM and PM use following code in XML
<TextClock
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:format24Hour="HH:mm"
android:textColor="#color/gray_100"
android:textSize="60sp" />

xml namespaces in xsl transform - ignore blank?

I'm new to xsl and am trying to write a template to transform xml to html.
I have an xml document that begins
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns:autn="http://schemas.com/aci/"
xmlns="http://iptc.org/std/nar/2006-10-01/">
<name>Bob</name>
and my xsl template begins
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:autn="http://schemas.autonomy.com/aci/">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
...
<body>
<p>user name:</p>
<p><xsl:value-of select="data/name"/></p>
The problem is, if I do
I don't get anything back for the value-of select.
If I do
I get 'Bob' but I lose all my html.
What am I missing?
You are missing the default namespace of the XML document:
xmlns="http://iptc.org/std/nar/2006-10-01/"
Add it to the XSLT as well:
<xsl:stylesheet version="1.0"
xmlns:mynamespace="http://iptc.org/std/nar/2006-10-01/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:autn="http://schemas.autonomy.com/aci/">
And use that namespace in the xsl:value-of:
<xsl:value-of select="mynamespace:data/mynamespace:name" />