2

我有 xml 我想按原样复制(检查 xmlns="" 和标签。我想按原样创建。

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

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

任何提前帮助非常感谢 rameshkumar singh

输入.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>
4

2 回答 2

3

这是你如何做到的:

<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在转换文档上声明命名空间。

于 2012-05-16T22:36:46.337 回答
0

这个简短而简单的转换(具有最少数量的模板并且不使用任何显式 XSLT 条件指令,没有xsl:attributepriority属性):

<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>

应用于提供的 XML 文档时:

<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>
于 2012-05-17T04:42:33.920 回答