开发者

XSLT help in declaring namespace

开发者 https://www.devze.com 2023-01-16 23:36 出处:网络
need some help in resolving the following issue. I need to transform the below input(XML) to the mentioned output(XML).

need some help in resolving the following issue. I need to transform the below input(XML) to the mentioned output(XML).

   <Header>
      <End_Date xsi:nil="true"/>
   <Header>

To the following format.

   <Header>
      <End_Date xsi:nil="true" xmlns:xsi"http://www.w3.org/2001/XMLSchema"/>
   <Header>

This is the stylesheet:

<xsl:style开发者_如何学Csheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs">  

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>  

    <xsl:template match="/">  
      <HEADER>  
        <xsl:for-each select="HEADER">  
          <xsl:sequence select="(./@node(), ./node())"/>
        </xsl:for-each>  
      </HEADER>  
    </xsl:template> 
</xsl:stylesheet>

Thanks in advance. Gabriel


Am I right in thinking you want to reproduce a nearly exact copy of the input XML, with the addition of the xsi namespace declaration that is lacking from the input?

First, as it is now, your input is not well-formed XML, just because of the lacking xsi namespace declaration. Hence, there's no way to use XSLT for adding it: any XSLT processor will choke on the input's non-well-formedness.

Second, you have to check case sensitivity: currently, no input nodes are matched by the <xsl:for-each select="HEADER"> select expression. If you change it to "Header", the template rule will indeed replace the <Header> input with <HEADER>, whose content is copied identically. But... only if you have the namespace declarations in the input right...

So, if the purpose is indeed to 'upgrade' non-well-formed XML to a well-formed version, I'd suggest to look for other tools, such as Perl, Awk, or any other simple search/replace solution that operates on plain text and could just add the missing namespace declaration to the document element:

<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <End_Date xsi:nil="true"/>
</Header>

(Of course, you could also make use of XSLT 2.0's unparsed-text($href) function that lets you read any file as unparsed text, which you could then further process with <xsl:analyze-string>. See Michael Kay's article Up-conversion using XSLT 2.0 for further inspiration. Since this a rather awkward way to process non-XML with an XML tool, I give this merely for completeness -- if adding the namespace prefix is the only problem to be solved, I'd definitely go for a cheaper search/replace option.)

Hope this helps,

Ron

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号