使用XSLT移除XML中的子节点并将其数据复制到父节点

时间:2019-07-17 13:51:20

标签: xml xslt xslt-1.0

我正在尝试消除所有子节点并将所有数据复制到父节点,但输出与输入相同。

输入XML-

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>    
    <Header>
    </Header>
    <Person>
      <Personal>
         <FirstName>abc</FirstName>
         <LastName>cde</LastName>
         <ID>12345</ID>
      </Personal>
      <AddressData>
         <Address1>abc123</Address1>
         <Address2>def345</Address2>
      </AddressData>
      <PhoneData>
         <Phone1>111111111</Phone1>
      </PhoneData>
    </Person>
 </PersonData>

我已经尝试了下面的代码,但是输出与输入相同,因此不会删除子节点,并且其中的数据也不会移动到父节点(即Person)。

   <?xml version='1.0'?>
   <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*" />

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

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

   </xsl:stylesheet>    

所需的输出-

   <?xml version="1.0" encoding="ISO-8859-1"?>
   <PersonData>    
   <Person>
     <FirstName>abc</FirstName>
     <LastName>cde</LastName>
     <ID>12345</ID>
     <Address1>abc123</Address1>
     <Address2>def345</Address2>
     <Phone1>111111111</Phone1>
    </Person>
    </PersonData>

我得到与输入XML相同的输出,而不是没有子节点的上述预期输出

1 个答案:

答案 0 :(得分:1)

怎么样:

XSLT 1.0

In [23]: whitelist = set(string.ascii_letters)                                                                                                                                                                                                                                                                                

In [24]: rev1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block. It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm
    ...:  back in town."                                                                                                                                                                                                                                                                                                      

In [25]: ''.join([char for char in rev1 if char in whitelist])                                                                                                                                                                                                                                                                
Out[25]: 'MygreatauntiehaslivedatEvertonParkfordecadesandonceuponatimeIevenlivedheretooandIrememberthedaysbeforewhentherewasnothingremotelyhipsteraboutthishousingblockItisreallycooltoseecutenewcafesandcoffeeshopsmovinginandIvebeentoNyloneverytimeImbackintown'

In [26]: whitelist = set(string.ascii_letters + ' ')                                                                                                                                                                                                                                                                          

In [27]: ''.join([char for char in rev1 if char in whitelist])                                                                                                                                                                                                                                                                
Out[27]: 'My great auntie has lived at Everton Park for decades and once upon a time I even lived here too and I remember the days before when there was nothing remotely hipster about this housing block It is really cool to see cute new cafes and coffee shops moving in and Ive been to Nylon every time Im back in town'