【问题标题】:Access to elements in xml file and path to them - PERL访问 xml 文件中的元素及其路径 - PERL
【发布时间】:2015-04-23 09:47:16
【问题描述】:

我需要访问 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>

【问题讨论】:

  • 你使用什么模块? XML::TwigXML::LibXML?
  • 对于以前的 xml,我使用了 XML::LibXML 和 XML::Simple ...但手动提取元素感觉不对...我也使用 XML::XPath、XML::XPath: .XMLParser
  • "每一个不为空的元素,我都想要它的值和路径。这样的事情可能吗?" 是的,这是可能的。请发布您期望获得的确切代码。如果您尝试过任何事情,也请发布。如果你没有,为什么不呢?
  • 除了使用 xml::simple 手动解析之外,我还没有写任何东西。不是很好,我只是在研究选项……我的 $parser = new XML::Simple; #parse 响应:我的 $xml = $parser->XMLin($file); $xml->{Control_Data}->{Document_Source};
  • 我们需要查看您的预期输出(XML?HTML 表格?文本?)。 “不为空的元素”也是模棱两可的:您的示例中的&lt;Contact_Name&gt; 是“空的”吗?它包含其他(空)元素。

标签: xml perl parsing xslt xpath


【解决方案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"

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多