You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改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. The exclude-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.
  • 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).
  • 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 using xsl:value-of.

Just swap out the namespace URIs, element names, and padding length to fit your exact requirements!

内容的提问来源于stack exchange,提问作者user1621702

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:19:25