2015-04-23 25 views
-1

我需要訪問xml中的所有子元素,如下所示。每一個非空的元素,我都想要它的價值和路徑。這是否有可能?我可以在xml :: simple中寫入手動解析,但它感覺不對。訪問xml文件中的元素和路徑 - PERL

<Incident> 
    <Organisation> 
     <USD_Local>Secure Cloud Container</USD_Local> 
    </Organisation> 
    <Location/> 
    <Contact_Reference> 
     <Organisation> 
     <USD_Local>Secure Cloud Container</USD_Local> 
     </Organisation> 
     <Contact_ID/> 
     <Contact_Name> 
     <First_Name/> 
     <Last_Name/> 
     </Contact_Name> 
     <External_Reference>AMZZEMP:000000000152299</External_Reference> 
     <Freeform_Details/> 
     <Email_Address/> 
    </Contact_Reference> 
    <Requested_By> 
     <Organisation> 
     <USD_Local>Secure Cloud Container</USD_Local> 
     </Organisation> 
     <Contact_ID/> 
     <Contact_Name> 
     <First_Name/> 
     <Last_Name/> 
     </Contact_Name> 
     <External_Reference>AMZZEMP:000000000152299</External_Reference> 
     <Freeform_Details/> 
     <Email_Address/> 
     <Additional_Attribute>Security-AAHPS.Other</Additional_Attribute> 
    </Requested_By> 
    <Incident_Type>Incident</Incident_Type> 
    <Severity>2</Severity> 
    <Category> 
     <USD_Local>Security-AAHPS.Other</USD_Local> 
     <USD_Foreign/> 
    </Category> 
    <Service_Type> 
     <Name/> 
     <Target_Duration/> 
    </Service_Type> 
    <Group> 
     <USD_Local>UK.Security.SOC</USD_Local> 
    </Group> 
    <Configuration_Item action="Add_Link"> 
     <Name>CATPLD2PRSSEN03</Name> 
     <CI_Number/> 
     <Serial_Number/> 
     <System_Name/> 
     <External_Reference/> 
     <Additional_Attribute/> 
    </Configuration_Item> 
    <Summary>Topbanana</Summary> 
    <Customer_Reference>SCC</Customer_Reference> 
    <Supplier_Reference/> 
    <Occurred_Date_Time format="yyyy/MM/dd HH:mm:ss">2015/04/01 10:50:52</Occurred_Date_Time> 
    </Incident> 
+0

你用什麼模塊? [XML :: Twig](http://p3rl.org/XML::Twig)或[XML :: LibXML](http://p3rl.org/XML::LibXML)? – choroba

+0

爲以前的XML我使用XML :: LibXML和XML ::簡單...但手動提取元素不覺得正確...我也使用XML :: XPath,XML :: XPath:.XMLParser – palyxk

+0

「*每個元素這不是空的,我想有它的價值和路徑,是否有可能這樣?*「是的,這是可能的。請發佈您期望得到的確切代碼。如果您嘗試過任何操作,請將其發佈。如果你沒有,爲什麼不呢? –

回答

1

如果我正確理解您的要求,可以很簡單地處理它;以下樣式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" encoding="UTF-8"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="text()"> 
    <xsl:for-each select="ancestor::*"> 
     <xsl:text>/</xsl:text> 
     <xsl:value-of select="name()"/> 
    </xsl:for-each> 
    <xsl:text>: "</xsl:text> 
    <xsl:value-of select="."/> 
    <xsl:text>"&#10;</xsl:text> 
</xsl:template> 

</xsl:stylesheet> 

當應用到您的示例XML輸入,將返回:

/Incident/Organisation/USD_Local: "Secure Cloud Container" 
/Incident/Contact_Reference/Organisation/USD_Local: "Secure Cloud Container" 
/Incident/Contact_Reference/External_Reference: "AMZZEMP:000000000152299" 
/Incident/Requested_By/Organisation/USD_Local: "Secure Cloud Container" 
/Incident/Requested_By/External_Reference: "AMZZEMP:000000000152299" 
/Incident/Requested_By/Additional_Attribute: "Security-AAHPS.Other" 
/Incident/Incident_Type: "Incident" 
/Incident/Severity: "2" 
/Incident/Category/USD_Local: "Security-AAHPS.Other" 
/Incident/Group/USD_Local: "UK.Security.SOC" 
/Incident/Configuration_Item/Name: "CATPLD2PRSSEN03" 
/Incident/Summary: "Topbanana" 
/Incident/Customer_Reference: "SCC" 
/Incident/Occurred_Date_Time: "2015/04/01 10:50:52" 
+0

哇!我曾經想過xlst,但不是這樣...這太棒了... – palyxk