XSLT and Java: Default Namespace XMLNS - java

I'm new to XSLT and I'm trying to transform one XML file into another one. My problem is that there is one namespace 'xmlns' without any prefix in the original xml file and when I transform it to another one through xslt then nothing happens but if I remove that xmlns namespace then it works out but actually I cannot modify the original .xml file since I'm bound to use that file only, so I have to keep xmlns in the original file as it is. So can anyone please suggest some modifications in my .xsl or java code to overcome this problem.
My Original XML namespace looks like-
<?xml version="1.0" encoding="UTF-8"?>
<manifest identifier="eXeorm_sample4823c6301f29a89a4c1f"
xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
</manifest>
My desired xml is:
<?xml version="1.0" encoding="UTF-8"?>
<manifest identifier="eXescorm_quiz4823c6301f29a8419515"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
</manifest>
My Modified XSLT-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1"
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="ims:manifest">
</xsl:stylesheet>

There's a search box at the top of this page: type "XSLT default namespace" and you will find hundreds of answers to this question.
Incidentally, your code is incredibly verbose. Instead of this:
<xsl:element name="item">
<xsl:attribute name="identifier">ITEM-eXeorm_sample4823c6301f29a89a4d27</xsl:attribute>
<xsl:attribute name="isvisible">true</xsl:attribute>
<xsl:attribute name="identifierref">RES-eXeorm_sample4823c6301f29a89a4d28</xsl:attribute>
</xsl:element>
use this:
<item identifier="ITEM-eXeorm_sample4823c6301f29a89a4d27" invisible="true"
identifierref="RES-eXeorm_sample4823c6301f29a89a4d28"/>

Related

Fastest way to check for comments and processing instructions in XML file and strip whitespace?

What's the most performant way to check for comments and processing instructions in an XML file and strip unnecessary whitespace in Java?
Processing should fail if either comment or processing instruction is contained in the XML file otherwise all unnecessary whitespace should be removed.
How would the solution look like if processing instructions and comments should be removed as well instead of failing the validation / transformation?
You could use this xslt (version 1.0):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|text()|*">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction()|comment()"/>
</xsl:stylesheet>
Unnecessary whitespace is somewhat tricky sinds in a mixed-content-model whitespace can be significant.

How to retrieve duplicate nodes from XML using xpath or java script

Below is my input xml.
<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
<Records>
<Org_Unit_Name>ABC</Org_Unit_Name>
<Parent_Org_Unit>123</Parent_Org_Unit>
</Records>
<Records>
<Org_Unit_Name>ABC</Org_Unit_Name>
<Parent_Org_Unit>DEF</Parent_Org_Unit>
</Records>
<Records>
<Org_Unit_Name>456</Org_Unit_Name>
<Parent_Org_Unit>879</Parent_Org_Unit>
</Records>
</Hierarchy>
I would like to extract only duplicate values. so the output should be as below
<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
<Records>
<Org_Unit_Name>ABC</Org_Unit_Name>
<Parent_Org_Unit>123</Parent_Org_Unit>
</Records>
<Records>
<Org_Unit_Name>ABC</Org_Unit_Name>
<Parent_Org_Unit>DEF</Parent_Org_Unit>
</Records>
I tried preceding axes in xpath/xslt, but of no use and unique(false) in java script but I am unable to retrieve the expected output. Please guide me how to proceed forward.
Regards,
Amuktha
Assuming that "duplicate" means a Records element with the same Org_Unit_Name value, you could use a variation of Muenchian grouping:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="Records-by-Org_Unit_Name" match="Records" use="Org_Unit_Name" />
<xsl:template match="/Hierarchy">
<xsl:copy>
<xsl:copy-of select="Records[count(key('Records-by-Org_Unit_Name', Org_Unit_Name)) > 1]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Java Function in XSLT Transformation

I have this tag on XML that I need use LUHN Algoritm input xml:
<?xml version="1.0" encoding="UTF-8"?>
<tag>urn:epc:id:sgtin:0614141.100013.000000000001</tag>
I have the java code read:
public class Luhn {
public static String calculateCheckDigit(String card) {
.....
}
The code is runing perfect but now I need call the function calculateCheckDigits inside XSLT to make the transformation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="java:sumCheck.Luhn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xmlLuhn>
<xsl:variable name="digits" select="."/>
Variable Digits:urn:epc:id:sgtin:0614141.100013.000000000001
<xsl:variable name="Luhn13Digits" select="translate(concat('0',substring($digits,19,13)),'.','')"/>
Variable Luhn13Digits = 00614141100013
<xmlLuhn2>
<xsl:value-of select="java:calculateCheckDigit($Luhn13Digits)"/>
</xmlLuhn2>
</xmlLuhn>
</xsl:template>
It´s not working, do you have idea how to make it ?
I already import the .jar of Luhn on the xslt project in Eclipse but nothing happens.

Java XSL Transformation complaining about binding prefix to namespace

I try to transform a XML-document which is a TCX-file from a Garmin Watch with XSL and the javax.xml.transform.Transformer. This is the document (shorten):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Running">
<Id>2014-08-23T15:36:15.000Z</Id>
<Lap StartTime="2014-08-23T11:50:34.000Z">
<TotalTimeSeconds>3181.0</TotalTimeSeconds>
<DistanceMeters>7620.0</DistanceMeters>
<MaximumSpeed>21.887998580932617</MaximumSpeed>
<Calories>386</Calories>
<AverageHeartRateBpm>
<Value>119</Value>
</AverageHeartRateBpm>
<MaximumHeartRateBpm>
<Value>167</Value>
</MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2014-08-23T11:50:34.375Z</Time>
<Position>
<LatitudeDegrees>48.17259333333333</LatitudeDegrees>
<LongitudeDegrees>10.191565</LongitudeDegrees>
</Position>
<AltitudeMeters>542.5505981445312</AltitudeMeters>
<DistanceMeters>0.0</DistanceMeters>
<HeartRateBpm>
<Value>70</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
</Trackpoint>
[...]
</Track>
<Notes>Note</Notes>
</Lap>
<Notes>Note</Notes>
<Training VirtualPartner="false">
<Plan Type="Workout" IntervalWorkout="false">
<Name>Running</Name>
<Extensions/>
</Plan>
</Training>
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
<Name>Polar V800</Name>
<UnitId>0</UnitId>
<ProductID>13</ProductID>
</Creator>
</Activity>
</Activities>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Application_t">
<Name>no application</Name>
</Author>
</TrainingCenterDatabase>
I use the javax.xml.transform.Transformer for this task and get a strange error. The transformer gives me the following error message:
Element "xmlns:xsi" cannot have "xmlns" as its prefix.
It belongs to the line:
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
From my point of view, this line is valid. The given namespace is just bound to the prefix xsi. Is it necessary to configure the Transformer to detect it right or do I need to specify something in the xsl-file?
Thanks

Embedding XSL Stylesheet into XML

I have the following XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="http://www.fakedomain.com/sally.xsl"?>
And the following content in sally.xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="documentcollection/document">
<p>
<xsl:for-each select="rss/channel/item">
<xsl:value-of select="title"/><br />
<xsl:value-of select="description"/><br />
<xsl:value-of select="link"/><br />
</xsl:for-each>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
However, the browser displays the XML as though the XSL line is not present. Do you know why the browser is ignoring the XSL stylesheet? Is the style sheet wrong?
Thanks
I have the following XML:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="http://www.fakedomain.com/sally.xsl"?>
This isn't a well-formed XML document (no top element present), so it isn't much of a surprize the browser doesn't treat it as such.
Solution:
Update your "XML" to a really well-formed XML document, something like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="file:///c:/temp/delete/xxx.xsl"?>
<t/>
With this stylesheet in c:\temp\delete\xxx.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
XXX
</xsl:template>
</xsl:stylesheet>
when the XML file is opened with IE, the browser displays the result of the transformation:
XXX
Looks like your not closing off one of your for-each tags?
the xsl:for-each loop of select="rss/channel/item" is not closed.
It could be same origin policy security restrictions. If your XML and XSLT are not hosted in the same location, then the browser may be refusing to fetch the XSLT and apply it to your XML file.

Categories