2012-05-16 284 views
1

我有XML我想要复制的是像(检查的xmlns =“”和标签。我想创建原样。XSLT空白命名空间

总的计算是照顾。只有这个问题。它是有效的。仍然客户希望预期的格式是这样的。任何帮助非常感谢。 三项任务

1)我需要添加命名空间员工xmnls =“1.2”xmlns:xsi =“3”xsi:schemalocation =“4 2)在输出xml中生成像这样的标签不是 3)需要避免xmlns =“”

任何帮助提前gr eatly赞赏 rameshkumar辛格

Input.xml中

<Employees> 
      <employee> 
      <dept>1</dept> 
       <sec></sec> 
      </employee> 
      <employee> 
       <dept>2</dept> 
       <sec></sec> 
      </employee> 
    </Employees> 

Expected.XML 

     <Employees xmnls="1.2" xmlns:xsi="3" xsi:schemalocation="4"> 
      <totalemp>2</totalemp> 
      <employee> 
       <dept>1</dept> 
       <sec></sec> 
      <employee> 
       <employee> 
        <dept>2</dept> 
        <sec></sec> 
       <employee> 
       </Employees> 


actual.XML 
       <Employees> 
        <totalemp>2</totalemp> 
         <employee xmlns=""> 
         <dept>1</dept> 
          <sec/> 
         </employee> 
         <employee> 
          <dept>2</dept> 
           <sec/> 
          <employee> 
       </Employees> 
+0

您可以直接生成正确的XML,而不是尝试使用XSLT进行转换吗? (顺便说一句,你的“移位”键似乎被打破,因为句子有随机大小写) –

回答

2

这里是你如何做到这一点:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="3"> 

    <xsl:output indent="yes"/> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*" priority="2"> 
     <xsl:element name="{local-name()}" namespace="1.2"> 
      <xsl:if test="self::Employees"> 
       <xsl:attribute name="xsi:schemalocation">4</xsl:attribute>   
      </xsl:if> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

您应用的身份转变为默认值,然后重写它的元素给他们提供了一个新的名称空间以及Employees节点的特殊属性。我选择添加if声明,但您也可以将该逻辑移入与Employees匹配的另一个模板。我只是不想重复整个xsl:element的事情两次。味道真的很重要。

当我将此转化为您的输入文档我结束了:

<Employees xmlns="1.2" xmlns:xsi="3" xsi:schemalocation="4"> 
    <employee> 
     <dept>1</dept> 
     <sec/> 
    </employee> 
    <employee> 
     <dept>2</dept> 
     <sec/> 
    </employee> 
</Employees> 

你必须在你的结果可能是xmlns=""因为你没有重新创建在新的命名空间的所有元素。此外,为了能够添加xsi:schemalocation属性,您需要在转换文档上声明xsi命名空间。

0

这种短期和简单的转换(有模板的最小数量,并且不使用任何明确的XSLT条件指令,没有xsl:attributepriority属性):当所提供的XML文档应用

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="3" xsi:schemalocation="4"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()[not(self::*)]|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:element name="{name()}" namespace="1.2"> 
    <xsl:copy-of select= 
    "document('')/*[not(current()/../..)] 
         /@xsi:schemalocation"/> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

<Employees> 
    <employee> 
     <dept>1</dept> 
     <sec></sec> 
    </employee> 
    <employee> 
     <dept>2</dept> 
     <sec></sec> 
    </employee> 
</Employees> 

产生想要的,正确的结果:

<Employees xmlns="1.2" xmlns:xsi="3" xsi:schemalocation="4"> 
    <employee> 
     <dept>1</dept> 
     <sec/> 
    </employee> 
    <employee> 
     <dept>2</dept> 
     <sec/> 
    </employee> 
</Employees> 
+0

我预计结果为 不是

+0

@RameshSingh:两者是等价的。两者都代表没有孩子的“秒”元素。精确的词汇表示不能在转换中进行控制,而且通常在一个XSLT处理器与另一个XSLT处理器之间存在差异。如果这对你来说真的很重要,你可以通过以下方式欺骗XSLT处理器:'' –

+0

我不能欺骗它,在我的真实需求中,我有大约1000个这样的xml标签,我的客户想用这种方式标签。我得到xmlns =“”问题。我不知道如何一起处理这两个问题 –