Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将元素分组到xslt中的新节点下_Xslt_Grouping - Fatal编程技术网

将元素分组到xslt中的新节点下

将元素分组到xslt中的新节点下,xslt,grouping,Xslt,Grouping,我需要将一些元素组合在一起,并将它们放在新元素下 下面是一个示例记录,我想将地址信息组合到一个附加层中 这是原始记录- <Records> <People> <FirstName>John</FirstName> <LastName>Doe</LastName> <Middlename /> <Age>20</Age>

我需要将一些元素组合在一起,并将它们放在新元素下

下面是一个示例记录,我想将地址信息组合到一个附加层中

这是原始记录-

  <Records>
    <People>
     <FirstName>John</FirstName>  
     <LastName>Doe</LastName>  
     <Middlename />
     <Age>20</Age>  
     <Smoker>Yes</Smoker>  
     <Address1>11 eleven st</Address1>  
     <Address2>app 11</Address2>
     <City>New York</City>
     <State>New York</State>
     <Status>A</Status>
    </People>
  </Records>

约翰
雌鹿
20
对
11 11街
应用程序11
纽约
纽约
A.
预期结果:我需要将地址数据分组到新元素下-

  <Records>
    <People>
     <FirstName>John</FirstName>  
     <LastName>Doe</LastName>  
     <Middlename />
     <Age>20</Age>  
     <Smoker>Yes</Smoker>
     <Address>       
       <Address1>11 eleven st</address1>  
       <Address2>app 11</address2>
       <City>New York</City>
       <State>New York</State>
     </Address>       
     <Status>A</Status>
    </People>
  </Records>

约翰
雌鹿
20
对
11 11街
应用程序11
纽约
纽约
A.
任何帮助都会很好!谢谢

这应该可以做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

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

  <xsl:template match="Smoker">
    <xsl:call-template name="copy" />
    <Address>
      <xsl:apply-templates select="../Address1 | 
                                   ../Address2 | 
                                   ../City | 
                                   ../State" />
    </Address>
  </xsl:template>
</xsl:stylesheet>

在输入XML上运行时,将生成:

<Records>
  <People>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <Middlename />
    <Age>20</Age>
    <Smoker>Yes</Smoker>
    <Address>
      <Address1>11 eleven st</Address1>
      <Address2>app 11</Address2>
      <City>New York</City>
      <State>New York</State>
    </Address>
    <Status>A</Status>
  </People>
</Records>

约翰
雌鹿
20
对
11 11街
应用程序11
纽约
纽约
A.
这应该可以做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

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

  <xsl:template match="Smoker">
    <xsl:call-template name="copy" />
    <Address>
      <xsl:apply-templates select="../Address1 | 
                                   ../Address2 | 
                                   ../City | 
                                   ../State" />
    </Address>
  </xsl:template>
</xsl:stylesheet>

在输入XML上运行时,将生成:

<Records>
  <People>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <Middlename />
    <Age>20</Age>
    <Smoker>Yes</Smoker>
    <Address>
      <Address1>11 eleven st</Address1>
      <Address2>app 11</Address2>
      <City>New York</City>
      <State>New York</State>
    </Address>
    <Status>A</Status>
  </People>
</Records>

约翰
雌鹿
20
对
11 11街
应用程序11
纽约
纽约
A.