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

XML转XML的XSLT样式表开发咨询:添加命名空间前缀并解决格式异常问题

Solution to Your XSLT Transformation Issue

Let's fix your XSLT step by step. You're running into problems with namespace prefixes (a:), incorrect namespace attribute syntax, and attribute ordering. Below is a complete, working stylesheet that produces the exact output you need, including valid namespace declarations and proper attribute ordering:

Working XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://127.0.0.1:8080"
                exclude-result-prefixes="xsd xsi"
                version="2.0">

    <xsl:output method="xml" indent="no" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <!-- Match root and create the a:Document element with all required namespaces -->
    <xsl:template match="/">
        <a:Document xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://127.0.0.1:8080 http://127.0.0.1:8080/test/tbattributesxml2.xsd"
                    xmlns:a="http://127.0.0.1:8080">
            <xsl:apply-templates select="Document/StartOfTransmission"/>
        </a:Document>
    </xsl:template>

    <!-- Convert StartOfTransmission to an attribute-based element with a: prefix -->
    <xsl:template match="StartOfTransmission">
        <a:StartOfTransmission>
            <!-- Convert child elements to attributes -->
            <xsl:attribute name="SenderId" select="SenderId"/>
            <xsl:attribute name="ReceiverId" select="ReceiverId"/>
            
            <!-- Format transdatetime correctly -->
            <xsl:variable name="formatted-date" select="concat(substring(TRANSDATE,7,4), '-', substring(TRANSDATE,4,2), '-', substring(TRANSDATE,1,2))"/>
            <xsl:attribute name="transdatetime" select="concat($formatted-date, 'T', TRANSTIME, '.000')"/>
            
            <xsl:attribute name="SenderReference" select="SenderReference"/>
            <xsl:attribute name="ReceiverReference" select="ReceiverReference"/>
            <xsl:attribute name="ApplicationReference" select="ApplicationReference"/>
        </a:StartOfTransmission>
    </xsl:template>

</xsl:stylesheet>

Key Fixes & Explanations

  • Proper Namespace Handling: Instead of trying to build namespace attributes with string concatenation (which causes invalid XML syntax), we directly declare the a: namespace on the root a:Document element. This ensures the prefix is valid and propagated correctly to child elements.
  • Explicit Element Creation: We don't use xsl:copy for the root Document or StartOfTransmission elements—instead, we create new elements with the a: prefix, which aligns with your target output's namespace requirements.
  • Controlled Attribute Order: By explicitly defining attributes in the order you need (starting with xmlns:xsd), we ensure the output matches your desired structure exactly.
  • Simplified Date-Time Formatting: The date-time conversion logic is streamlined while maintaining the required xs:dateTime format (e.g., 2010-09-13T12:00:00.000).

Verification

When applied to your source XML, this stylesheet produces output that fully complies with the provided XSD schema, including all required attributes, the a: namespace prefix, and valid namespace declarations.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 21:39:06