History of XML-to-XML Conversion (with Math or Logic)
The process of converting XML to XML evolved as part
of the broader history of structured data exchange and data
transformation standards. Here's a concise yet informative timeline and
background showing how it developed — especially where mathematical logic
fits in.
Origins of XML
1998: XML 1.0 W3C
Recommendation
- XML
(Extensible Markup Language) was introduced by the W3C as a text-based,
self-descriptive format to store and transport data.
- Designed
to be machine-readable and human-readable.
- Inspired
by SGML (Standard Generalized Markup Language).
The Need for XML-to-XML Transformation
As XML became widespread in:
- E-commerce
(e.g., invoices, catalogs),
- Finance
(e.g., trade data, tax calculations),
- Government
(e.g., e-filing, permits),
...different systems needed to exchange XML in different structures.
But transforming raw data wasn’t enough — they needed to compute,
aggregate, and apply rules (like tax, subtotal, discount) during
transformation.
Hence: XML ➡️ XML + Math logic
transformation was born.
Key Technologies
and Milestones
1. XSLT (Extensible Stylesheet Language Transformations)
- Released:
XSLT 1.0 in 1999
- Created
by W3C for transforming XML into another XML, HTML, or text.
- Based
on pattern matching and template rules.
- Supports
math operations using XPath:
xml
|
<xsl:value-of
select="price * quantity"/> |
- Limited
to basic arithmetic, lacks advanced math/statistics.
2. XPath (XML Path Language)
- Works
with XSLT to select nodes and apply math operations.
- Supports
functions like sum(), count(), floor(), round(), etc.
- Introduced
a declarative, functional approach to XML logic.
3. XQuery
- Released:
W3C Recommendation in 2007
- A
full-fledged query language for XML (like SQL for relational
databases).
- Offers:
- Looping
- Complex
expressions
- Advanced
numeric functions
- Widely
used for XML transformations with advanced logic (e.g., financial
computations).
4. Java/XML APIs (DOM, SAX, JAXB)
- For
programmatic control over XML transformation.
- Allowed
complex business logic, data mapping, and custom math operations.
- Enabled
real-world XML math transformations:
- Inventory
valuation
- EMI/NPV
calculations
- Performance
metrics generation
5. XSLT 2.0 and 3.0 Enhancements
- Added
support for:
- Decimal
formatting
- Date/time
arithmetic
- Regular
expressions
- Stronger
type checking
- Adopted
in enterprise and government data processing where math + XML logic
was vital.
Why Math Became Essential in XML-to-XML
XML wasn't just for structure — it carried semantic data
(e.g., price, tax, rating, volume). Converting this data from one system to
another required computing results, not just moving values.
Examples:
- Invoicing:
subtotal = price × quantity
- Finance:
NPV, IRR
- Analytics:
average, growth rate
- Rules:
if-else discount logic
🚀 Real-World Examples by
Domain
|
Domain |
XML Input |
Math/Logic |
XML Output |
|
E-Commerce |
<price>, <qty> |
subtotal = price × qty |
<total> |
|
Tax Filing |
<income>, <deductions> |
tax = slabs + percentage logic |
<taxLiability> |
|
Banking |
<principal>, <rate>,
<time> |
simple/compound interest |
<interest> |
|
Logistics |
<weight>, <distance> |
cost = rate × weight × distance |
<shippingCharge> |
|
Education |
<marks> |
average, grade mapping |
<result> |
Summary: Evolution
of XML-to-XML Math Logic
|
Era |
Tool/Tech |
Math Capability |
|
1998–2000 |
XML + XSLT 1.0 |
Basic arithmetic |
|
2000–2010 |
XPath/XQuery |
Math functions, loops |
|
2010–2020 |
XSLT 2.0/3.0, Java |
Complex rule-based math |
|
2020–Today |
REST + JSON + XML |
Hybrid transformation + AI logic |
Converting one XML format to another in Java typically
involves transforming the source XML using XSLT (Extensible Stylesheet Language
Transformations). XSLT is a powerful language for transforming XML documents
into other XML documents (or other formats like HTML, plain text, etc.).
Here's a step-by-step guide on how to perform XML to XML
transformation in Java using XSLT:
Step 1: Create the XSLT Stylesheet
First, you need to create an XSLT stylesheet that defines
how to transform the source XML to the target XML.
example.xsl:
xml
|
<?xml
version="1.0"?> <xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" />
<!-- Copy all content -->
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<!-- Make necessary modifications to the address -->
<xsl:template match="address">
<xsl:element
name="place-where-person-live">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<!-- Enclose the name within a <hubba> tag -->
<xsl:template match="name">
<xsl:element
name="name">
<nickName>
<xsl:apply-templates />
</nickName>
</xsl:element>
</xsl:template> </xsl:stylesheet> |
Step 2: Create the Source XML
Create the source XML file that you want to transform.
source.xml:
xml
|
<?xml
version="1.0"?>
<!-- This is a comment --> <people>
<address type="personal">
<name>Lars </name>
<street> Test </street>
<telephon
number="0123" />
</address>
<address type="personal">
<name>Joe </name>
<street> Test2
</street>
<telephon
number="1234" />
</address>
<address type="business">
<name>Jim</name>
<street> Test3
</street>
<telephon
number="2345" />
</address> </people> |
Step 3: Write the Java Code for Transformation
Use Java's built-in libraries to apply the XSLT stylesheet
to the source XML.
XmlTransformer.java:
java
|
import
javax.xml.transform.*; import
javax.xml.transform.stream.StreamResult; import
javax.xml.transform.stream.StreamSource; import
java.io.File; public class
XmlTransformer { public static void main(String[] args) { try { // Source XML document File xFile = new
File("source.xml"); // XSLT stylesheet document File xltFile = new
File("example.xsl"); // Generate Source objects for both the XML and XSLT documents Source xlSource = new
StreamSource(xFile); Source xltSource = new
StreamSource(xltFile); // Instantiate a TransformerFactory TransformerFactory
tnsFactory = TransformerFactory.newInstance(); // Create a Transformer for the
XSLT stylesheet Transformer tnsformer = tnsFactory.newTransformer(xltSource); // Establish a Result object for the transformed XML output File trnsXmlFile = new
File("transformed.xml"); Result result = new
StreamResult(trnsXmlFile); // Execute the transformation tnsformer.transform(xlSource,
result);
System.out.println("Transformation complete. Output saved to
" + transformedXmlFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } } |
Explanation
- Create
Source and Result Objects:
- Source
xmlSource = new StreamSource(xmlFile);
- Source
xsltSource = new StreamSource(xsltFile);
- Result
result = new StreamResult(transformedXmlFile);
- Create
TransformerFactory and Transformer:
- TransformerFactory
transformerFactory = TransformerFactory.newInstance();
- Transformer
transformer = transformerFactory.newTransformer(xsltSource);
- Perform
the Transformation:
- transformer.transform(xmlSource,
result);
Running the Code
- Save
the XSLT File: Save the XSLT content to example.xsl.
- Save
the Source XML File: Save the source XML content to source.xml.
- Save
the Java Code: Save the Java code to XmlTransformer.java.
- Compile
the Java Code: Open a terminal and navigate to the directory
containing the Java file. Compile the code using:
sh
|
javac
XmlTransformer.java |
- Run
the Java Code: Execute the compiled class using:
sh
|
XmlTransformer.java |
Output
|
<?xml
version="1.0" encoding="UTF-8"?><!-- This is a
comment --><people> <place-where-person-live>
<name><nickName>Lars </nickName></name> <street> Test
</street> <telephon
number="0123"/> </place-where-person-live> <place-where-person-live>
<name><nickName>Joe </nickName></name> <street> Test2
</street> <telephon
number="1234"/> </place-where-person-live> <place-where-person-live>
<name><nickName>Jim</nickName></name> <street> Test3
</street> <telephon
number="2345"/> </place-where-person-live> </people> |
Notes
- Ensure
that source.xml and example.xsl are in the same directory as XmlTransformer.java
or provide the correct file paths.
- Adjust
the XSLT stylesheet according to the structure of your source and target
XML formats.
- You
can extend the XSLT stylesheet to handle more complex transformations as
needed.
By following these steps, you can transform an XML document
from one format to another using Java and XSLT.
Ø
creating
a hierarchical diagram for cloud logging
Ø
A
hierarchical structure that includes a broader range of google cloud services
For Chemistry information, visit:
Ø
Molecular
weight of chemistry in Java code
Ø
To
generate a chemical formula look using HTML
Ø Orbitals and Electron Configuration Hund’s Rule
For Other information, visit
Ø
String
to xml or html Beautifier
Ø
How
to convert XML to Object and Object to XML
Ø
Convert
Floating-Point Values from SQL Server to Oracle in Java
0 Comments