代码之家  ›  专栏  ›  技术社区  ›  FrustratedWithFormsDesigner

oracleslt:默认名称空间导致空标记

  •  1
  • FrustratedWithFormsDesigner  · 技术社区  · 15 年前

    我认为问这个问题的最好方法是:如何为输出中的根元素指定默认名称空间?这样做:

    <xsl:template match="/">
            <r xmlns:s"http://www.mycompany.com/s/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mycompany.com/default/schema" >
    ....
    ....
    

    ORA-31011: XML Parsing Failed
    ORA-19201: Error occurred in in XML Processing
    LPX-00604: Invalid attribute 'nIfNotExist', for attribute 'name'
    ORA-06512: at SYS.XMLType at line 74 
    ORA-06512: at line 24
    

    'nIfNotExist'

     <xsl:template name="nIfNotExist" xmlns:scom="http://www.mycomapny.com/s/schema">
      <xsl:param name="nodeToTest"/>
      <xsl:param name="nodeName"/>
                    ...
    

    我希望结果文档的根元素如下所示:

    <r xmlns:s="http://www.mycompany.com/s/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mycompany.com/default/schema">
    

    我想要 "http://www.mycompany.com/default/schema" 作为默认名称空间,以便文档可以通过XSD验证。否则,我必须在运行验证之前手动添加它(不是批处理选项)。

    编辑

    我试过这个:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:s="http://www.mycompany.com/schema"
     xmlns="http://www.mycompany.com/def_schema">
    

    <r xmlns:s="http://www.mycompany.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mycompany.com/def_schema">
        <a></a>
        <s:b></s:b>
        <c></c>
        ....
    

    应该是:

    <r xmlns:s="http://www.mycompany.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mycompany.com/def_schema">
        <a>123</a>
        <s:b>ABC34L</s:b>
        <c>7.092381</c>
    

    源数据如下所示(我得到的输入中没有定义名称空间):

    <ROOT_NODE>
        <DATA_A>1234</DATA_A>
        <DATA_B>34567</DATA_B>
        <OTHER_DATA_C>7.123456</OTHER_DATA_C>
    </ROOT_NODE>
    

    期望输出

    <r xmlns:s="http://www.mycompany.com/schema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.mycompany.com/def_schema">
        <a>1234</a>
        <s:b>34567</s:b>
        <c>7.123456</c>
    </r>
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Dimitre Novatchev    15 年前

    这种转变

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:s="http://www.mycompany.com/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.mycompany.com/def_schema"
     >
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/*">
      <r>
       <xsl:apply-templates/>
      </r>
     </xsl:template>
    
     <xsl:template match="DATA_A">
      <a>
       <xsl:apply-templates/>
      </a>
     </xsl:template>
    
      <xsl:template match="OTHER_DATA_C">
       <c>
        <xsl:apply-templates/>
       </c>
      </xsl:template>
    
      <xsl:template match="DATA_B">
       <s:b>
        <xsl:apply-templates/>
       </s:b>
      </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的XML文档时 :

    <ROOT_NODE>
        <DATA_A>1234</DATA_A>
        <DATA_B>34567</DATA_B>
        <OTHER_DATA_C>7.123456</OTHER_DATA_C>
    </ROOT_NODE>
    

    产生想要的结果 :

    <r xmlns:s="http://www.mycompany.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mycompany.com/def_schema">
        <a>1234</a>
        <s:b>34567</s:b>
        <c>7.123456</c>
    </r>
    
        2
  •  1
  •   Robert Rossney    15 年前

    你已经找到正确答案的一半了。当您在转换的根目录下声明一个默认名称空间时,您正在断言(假设您没有覆盖其他地方的声明)该文档中的所有非限定元素都属于该名称空间。转换发出的每个非限定元素都将属于该名称空间。你说得对。

    我认为您可能忽略了XSLT转换中的命名空间声明

    你可能想这样做:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:s="http://www.mycompany.com/schema" 
     xnlns:no=""
     xmlns="http://www.mycompany.com/def_schema">
    

    <xsl:template match="no:foo">
       <foo>...</foo>
    </xsl:template>
    

    xsl:element

    <xsl:template match="no:*">
       <xsl:element name="{local-name()}">
          <xsl:apply-templates select="node()|@*"/>
       </xsl:element>
    </xsl:element>
    

    编辑:

    我以前从未以这种方式使用过名称空间,事实证明,上述内容实际上并不合法。不能使用命名空间前缀指定任何命名空间。因此,不能将目标命名空间用作转换的默认命名空间,因为这样就无法告诉XPath在源文档中查找元素。

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:s="http://www.mycompany.com/schema" 
     xmlns:out="http://www.mycompany.com/def_schema">
    

    …但那会写下 out 输出的名称空间前缀,这不会影响任何XML处理器,但可能会影响人类。也可以在模板中明确指定,例如:

    <xsl:template match="foo">
       <xsl:element name="foo" namespace="http://www.mycompany.com/def_schema">
          ...
       </xsl:element>
    </xsl:template>
    
        3
  •  0
  •   FrustratedWithFormsDesigner    15 年前