修改XSLT以将指定输入XML转换为目标输出XML
Alright, let's work through your XSLT transformation needs. I'll put together a concrete example that covers namespace mapping, ReferenceNumber padding with leading zeros, and other common transformation logic—just adjust it to match your exact input/output details!
Input XML Example
Let's assume your source XML looks something like this (adjust the namespace and elements to match your actual input):
<Root xmlns:legacy="http://your-old-namespace.com"> <legacy:Transaction> <legacy:ReferenceNumber>456</legacy:ReferenceNumber> <legacy:ItemName>Wireless Headphones</legacy:ItemName> <legacy:Quantity>2</legacy:Quantity> </legacy:Transaction> </Root>
Target Output XML
Here's the desired output we'll aim for, with proper namespace mapping and padded ReferenceNumber:
<TransactionBatch xmlns="http://your-target-namespace.com"> <TransactionRecord> <ReferenceNumber>00000456</ReferenceNumber> <ProductName>Wireless Headphones</ProductName> <Qty>2</Qty> </TransactionRecord> </TransactionBatch>
XSLT Implementation
This stylesheet handles all the required transformations:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:legacy="http://your-old-namespace.com" xmlns:target="http://your-target-namespace.com" exclude-result-prefixes="legacy"> <!-- Root template to start the transformation --> <xsl:template match="/"> <target:TransactionBatch> <xsl:apply-templates select="legacy:Root/legacy:Transaction"/> </target:TransactionBatch> </xsl:template> <!-- Transform the Transaction element and its children --> <xsl:template match="legacy:Transaction"> <target:TransactionRecord> <!-- Pad ReferenceNumber to 8 digits with leading zeros --> <target:ReferenceNumber> <xsl:value-of select="format-number(legacy:ReferenceNumber, '00000000')"/> </target:ReferenceNumber> <!-- Rename ItemName to ProductName --> <target:ProductName> <xsl:value-of select="legacy:ItemName"/> </target:ProductName> <!-- Rename Quantity to Qty --> <target:Qty> <xsl:value-of select="legacy:Quantity"/> </target:Qty> </target:TransactionRecord> </xsl:template> </xsl:stylesheet>
Key Logic Breakdown
Namespace Mapping:
- We declare both the original (
legacy) and target (target) namespaces at the top. Theexclude-result-prefixes="legacy"keeps the old namespace from cluttering the output. - All output elements use the
target:prefix, which maps to your desired namespace URI—this will appear as the default namespace in the final XML.
- We declare both the original (
ReferenceNumber Padding:
- The
format-number(legacy:ReferenceNumber, '00000000')function ensures the number is padded with leading zeros to reach 8 digits. If you need a different length, just adjust the number of zeros in the format string (e.g.,'0000000000'for 10 digits).
- The
Element Renaming & Value Copying:
- For elements that need a name change (like
ItemName→ProductName), we create the new element in the target namespace and copy the value from the original element usingxsl:value-of.
- For elements that need a name change (like
Just swap out the namespace URIs, element names, and padding length to fit your exact requirements!
内容的提问来源于stack exchange,提问作者user1621702

