访问xml文件中的元素及其路径 - PERL

时间:2015-04-23 09:47:17

标签: xml perl parsing xslt xpath

我需要访问xml中的所有子元素,如下所示。每个非空的元素,我都希望有它的价值和路径。有可能这样吗?我可以在xml :: simple中手动解析,但感觉不对。

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

1 个答案:

答案 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"