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 roota:Documentelement. This ensures the prefix is valid and propagated correctly to child elements. - Explicit Element Creation: We don't use
xsl:copyfor the rootDocumentorStartOfTransmissionelements—instead, we create new elements with thea: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:dateTimeformat (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
相关产品推荐
相关产品推荐

