0
我有下面的XML文件,我想使用R進行分析。XML具有很深的結構,並且也有不同數量的子節點。提取深層XML結構
<?xml version="1.0" encoding="UTF-8"?>
<Alert date="20161223_2" type="full">
<Records>
<Person Id="100">
<PersonNameDetails>
<PersonNames id="Name1">
<ReferenceGroup ReferenceGroupCode="ABC"/>
<ReferenceGroup ReferenceGroupCode="DEF"/>
<PersonNameValue>
<FirstName>Carl Bangouvounda</FirstName>
<Surname>Toziz</Surname>
</PersonNameValue>
</PersonNames>
<PersonNames id="Name2">
<ReferenceGroup ReferenceGroupCode="ABC"/>
<ReferenceGroup ReferenceGroupCode="GHI" ReferenceGroupLanguageCode="en"/>
<ReferenceGroup ReferenceGroupCode="JKL"/>
<ReferenceGroup ReferenceGroupCode="MNO"/>
<ReferenceGroup ReferenceGroupCode="DEF"/>
<PersonNameValue>
<FirstName>Tozize</FirstName>
<Surname>Bangouvonda</Surname>
</PersonNameValue>
</PersonNames>
<PersonNames id="Name3">
<ReferenceGroup ReferenceGroupCode="MNO"/>
<PersonNameValue>
<FirstName>Carol</FirstName>
<Surname>Tozize</Surname>
</PersonNameValue>
</PersonNames>
<PersonNames id="Name4">
<ReferenceGroup ReferenceGroupCode="PQR"/>
<ReferenceGroup ReferenceGroupCode="MNO"/>
<PersonNameValue>
<FirstName>Carol</FirstName>
<MiddleName>Bangouvonda</MiddleName>
<Surname>Tozize</Surname>
</PersonNameValue>
</PersonNames>
<PersonNames id="Name5">
<ReferenceGroup ReferenceGroupCode="GHI" ReferenceGroupLanguageCode="en"/>
<ReferenceGroup ReferenceGroupCode="JKL"/>
<ReferenceGroup ReferenceGroupCode="DEF"/>
<PersonNameValue>
<FirstName>Carl Bangouvonda</FirstName>
<Surname>Toziz</Surname>
</PersonNameValue>
</PersonNames>
</PersonNameDetails>
</Person>
</Records>
</Alert>
預期的輸出如下:
-----------------------------------------------------------
Id | id | ReferenceGroup | FirstName | MiddleName | Surname
-----------------------------------------------------------
100 | Name1 | ABC, DEF | Carl Bangouvounda | NA | Toziz
-----------------------------------------------------------
100 | Name2 | ABC, GHI, JKL, MNO, DEF | Tozize | NA | Bangouvonda
-----------------------------------------------------------
100 | Name3 | MNO | Carol | NA | Tozize
-----------------------------------------------------------
100 | Name4 | PQR, MNO | Carol | Bangouvonda | Tozize
-----------------------------------------------------------
100 | Name5 | GHI, JKL, DEF | Carl Bangouvonda | NA | Toziz
-----------------------------------------------------------
ID是元素人的屬性,且其他所有從PersonNameDetails。我也想將ReferenceGroupCode連接成同一個Personnames元素中的一個字符串。
我跟着建議轉換爲XSLT用下面的代碼:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Alert ">
<xsl:copy>
<xsl:apply-templates select="Records"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Records">
<xsl:apply-templates select="Person"/>
</xsl:template>
<xsl:template match="Person">
<xsl:apply-templates select="PersonNameDetails"/>
</xsl:template>
<xsl:template match="PersonNameDetails">
<xsl:apply-templates select="PersonNames"/>
</xsl:template>
<xsl:template match="PersonNames">
<xsl:apply-templates select="PersonNameValue"/>
</xsl:template>
<xsl:template match="PersonNameValue">
<PersonNameValue>
<Id><xsl:value-of select="ancestor::Person/@Id"/></Id>
<id><xsl:value-of select="ancestor::PersonNames/@id"/></id>
<xsl:copy-of select="FirstName"/>
<MiddleName><xsl:value-of select="MiddleName"/></MiddleName>
<Surname><xsl:value-of select="Surname"/></Surname>
<ReferenceGroupCode><xsl:value-of select="ancestor::PersonNames/ReferenceGroup/@ReferenceGroupCode"/></ReferenceGroupCode>
</PersonNameValue>
</xsl:template>
</xsl:transform>
如何更改XSLT代碼,以便ReferenceGroup輸出將是
<ReferenceGroupCode>ABC,DEF</ReferenceGroupCode>
任何幫助,高度讚賞。
我不希望將XML轉換爲XSLT。你能告訴我你需要什麼樣的信息來解決這個XMl解析問題嗎? –