1

いくつかの要素をグループ化して、新しい要素の下にまとめる必要があります。

以下はサンプルレコードです。アドレス情報を追加のレイヤーにグループ化したいと思います。

これが元のレコードです-

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

期待される結果:アドレスデータを新しい要素の下にグループ化する必要があります-

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

どんな助けでも素晴らしいでしょう!ありがとうございました

4

1 に答える 1

1

これはそれを行う必要があります:

<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>
于 2013-03-02T14:39:11.637 に答える