如何使用 PHP 脚本编辑 XML 文件格式


How to edit XML file format using PHP script

我需要制作一个php脚本,将一个xml文件重新格式化为另一个xml文件

最好的

方法是什么?simpleXML似乎是解决方案,但我无法从每个标签中提取值并将其放入正确的格式文件中。

$xml = simplexml_load_file("file.xml");
//print_r($xml);
$array = json_decode(json_encode((array)$xml), TRUE);

例如。

原始 XML

    <?xml version="1.0" encoding="ISO-8859-1"?>
<ADI>
    <Metadata>
        <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesPackageAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010000000019545827" Asset_Class="package"/>
        <App_Data App="MOD" Name="Metadata_Spec_Version" Value="CableLabsVOD1.1"/>
    </Metadata>
    <Asset>
        <Metadata>
            <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesTitleAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010100000019545827" Asset_Class="title"/>
            <App_Data App="MOD" Name="Type" Value="title"/>
            <App_Data App="MOD" Name="Title_Sort_Name" Value="Special Agent Oso"/>
            <App_Data App="MOD" Name="Title_Brief" Value="SpecialAgentOSO1-03"/>
            <App_Data App="MOD" Name="Title" Value="Special Agent Oso 1-03 A View To A Book/ Diamond's Are For Kites"/>
            <App_Data App="MOD" Name="Episode_Name" Value="A View To A Book/ Diamond's Are For Kites"/>
            <App_Data App="MOD" Name="Episode_ID" Value="Episode#03"/>
            <App_Data App="MOD" Name="Summary_Medium" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/>
            <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/>
            <App_Data App="MOD" Name="Summary_Short" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/>
            <App_Data App="MOD" Name="Rating" Value="TV-Y"/>
            <App_Data App="MOD" Name="Closed_Captioning" Value="Y"/>
            <App_Data App="MOD" Name="Run_Time" Value="00:24:04"/>
            <App_Data App="MOD" Name="Display_Run_Time" Value="00:24"/>
            <App_Data App="MOD" Name="Year" Value="2008"/>
            <App_Data App="MOD" Name="Country_of_Origin" Value="US"/>
            <App_Data App="MOD" Name="Studio" Value="Disney"/>
            <App_Data App="MOD" Name="Category" Value="Disney Junior On Demand/Special Agent OSO"/>
            <App_Data App="MOD" Name="Genre" Value="Adventure"/>
            <App_Data App="MOD" Name="Genre" Value="Mystery"/>
            <App_Data App="MOD" Name="Genre" Value="Animation"/>
            <App_Data App="MOD" Name="Box_Office" Value="0"/>
            <App_Data App="MOD" Name="Billing_ID" Value="1003"/>
            <App_Data App="MOD" Name="Licensing_Window_Start" Value="2014-11-07T00:00:00"/>
            <App_Data App="MOD" Name="Licensing_Window_End" Value="2014-11-20T23:59:59"/>
            <App_Data App="MOD" Name="Preview_Period" Value="0"/>
            <App_Data App="MOD" Name="Provider_QA_Contact" Value="abc@abc.com"/>
            <App_Data App="MOD" Name="Suggested_Price" Value="0.00"/>
        </Metadata>
        <Asset>
            <Metadata>
                <AMS Provider="" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesMovieAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="" Asset_ID="10010200000019545827" Asset_Class="movie"/>
                <App_Data App="MOD" Name="Type" Value="movie"/>
                <App_Data App="MOD" Name="Audio_Type" Value="Stereo"/>
                <App_Data App="MOD" Name="HDContent" Value="N"/>
                <App_Data App="MOD" Name="Viewing_Can_Be_Resumed" Value="N"/>
                <App_Data App="MOD" Name="Watermarking" Value="N"/>
                <App_Data App="MOD" Name="Copy_Protection" Value="N"/>
                <App_Data App="MOD" Name="Content_FileSize" Value=""/>
                <App_Data App="MOD" Name="Content_CheckSum" Value=""/>
            </Metadata>
            <Content Value="FOD31299.mpg"/>
        </Asset>
    </Asset>
</ADI>

重新格式化 XML 后

<?xml version="1.0">
<!DOCTYPE ADI SYSTEM "ADI.DTD">
<ADI>
  <Metadata>
    <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesPackageAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010000000019545827" Asset_Class="PACKAGE"/>
    <App_Data App="MOD" Name="Metadata_Spec_Version" Value="CableLabsVOD1.1"/>
  </Metadata>
  <Asset>
    <Metadata>
      <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOso1-03AViewToABook/Diamond'sAreForKitesTitleAsset" Version_Major="1" Version_Minor="0" Description="Special Agent Oso" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010100000019545827" Asset_Class="TITLE"/>
      <App_Data App="MOD" Name="Type" Value="title"/>
      <App_Data App="MOD" Name="Title_Brief" Value="SpecialAgentOSO1-03"/>
      <App_Data App="MOD" Name="Title" Value="SpecialAgentOSO1-03"/>
      <App_Data App="MOD" Name="Show_Type" Value="Series"/>
      <App_Data App="MOD" Name="Episode_Name" Value="A View To A Book/ Diamond's Are For Kites"/>
      <App_Data App="MOD" Name="Episode_ID" Value="03"/>
      <App_Data App="MOD" Name="Series_Name" Value="Special Agent Oso"/>
      <App_Data App="MOD" Name="Season_Number" Value="1"/>
      <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/>
      <App_Data App="MOD" Name="Summary_Long" Value="When Joe needs a book to identify the toy dinosaurs he's taking to school, Oso borrows a book from the library. /When Sarah wants to be a part of Kite Day but isn't sure how to fly a kite, Oso helps Sarah find a kite and get it in the air."/>
      <App_Data App="MOD" Name="Rating" Value="G"/>
      <App_Data App="MOD" Name="Closed_Captioning" Value="Y"/>
      <App_Data App="MOD" Name="Run_Time" Value="00:24:04"/>
      <App_Data App="MOD" Name="Display_Run_Time" Value="00:24"/>
      <App_Data App="MOD" Name="Year" Value="2008"/>
      <App_Data App="MOD" Name="Actors" Value="N/A"/>
      <App_Data App="MOD" Name="Director" Value="N/A"/>
      <App_Data App="MOD" Name="Canadian_Content" Value="0"/>
      <App_Data App="MOD" Name="Country_Of_Origin" Value="USA"/>
      <App_Data App="MOD" Name="Category" Value="Subscription/Family &amp; Kids/Disney Junior On Demand/Special Agent OSO"/>
      <App_Data App="MOD" Name="Genre" Value="Series/All"/>
      <App_Data App="MOD" Name="Genre" Value="Children"/>
      <App_Data App="MOD" Name="Billing_ID" Value="1003"/>
      <App_Data App="MOD" Name="Licensing_Window_Start" Value="2014-11-07T00:00:00"/>
      <App_Data App="MOD" Name="Licensing_Window_End" Value="2014-11-20T23:59:59"/>
      <App_Data App="MOD" Name="Preview_Period" Value="0"/>
      <App_Data App="MOD" Name="Maximum_Viewing_Length" Value="1440"/>
      <App_Data App="MOD" Name="Provider_QA_Contact" Value="abc@abc.com"/>
      <App_Data App="MOD" Name="Suggested_Price" Value="0.00"/>
    </Metadata>
    <Asset>
      <Metadata>
        <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataMovieAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Movie Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010200000019545827" Asset_Class="MOVIE"/>
        <App_Data App="MOD" Name="Encryption" Value="Y"/>
        <App_Data App="MOD" Name="Type" Value="movie"/>
        <App_Data App="MOD" Name="Audio_Type" Value="stereo"/>
        <App_Data App="MOD" Name="HDContent" Value="N"/>
        <App_Data App="MOD" Name="Copy_Protection" Value="Y"/>
        <App_Data App="MOD" Name="Languages" Value="en"/>
      </Metadata>
      <Content Value="FOD31299.mpg"/>
    </Asset>
    <Asset>
      <Metadata>
        <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataPreviewAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Preview Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010300000019545827" Asset_Class="PREVIEW"/>
        <App_Data App="MOD" Name="Type" Value="preview"/>
      </Metadata>
      <Content Value="FOD31299_preview.mpg"/>
    </Asset>
    <Asset>
      <Metadata>
        <AMS Provider="FAMILY" Product="MOD" Asset_Name="SpecialAgentOSO1-03MetadataPosterAsset" Version_Major="1" Version_Minor="0" Description="SpecialAgentOSO1-03 Metadata Poster Asset" Creation_Date="2014-10-20" Provider_ID="astral.com" Asset_ID="10010400000019545827" Asset_Class="POSTER"/>
        <App_Data App="MOD" Name="Type" Value="poster"/>
      </Metadata>
      <Content Value="FOD31299_poster.jpg"/>
    </Asset>
  </Asset>
</ADI>

几种可能的方法可以使用 xml。虽然您可以使用 DOM 和 SAX 方法,但转换 xml 的任务直接由 XSLT 技术解决。

编写 xsl-stylesheet 后,您可以使用 php 或独立的 xstl-处理器(如 SAXON)将其应用于原始 xml。

使用 XSLT 还可以从 IDE 支持中受益。像NetBeans,IntellijIDEA/PHPStorm这样的IDE提供了工具来舒适地编写和调试XSLT样式表。

考虑一下这个 XSLT,尽管它可能并不理想,因为我已经很久没有练习 XSLT 了:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" doctype-system="ADI.DTD"/>
    <xsl:template match="/">
        <xsl:element name="ADI">
            <xsl:apply-templates select="*"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="Metadata">
        <xsl:element name="Metadata">
            <xsl:apply-templates select="AMS"/>
            <xsl:for-each select="App_Data">
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
    <xsl:template match="Asset">
        <xsl:element name="Asset">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="Content">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="AMS">
        <xsl:element name="AMS">
            <xsl:apply-templates select="@*"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@Provider">
        <xsl:attribute name="Provider">
            <xsl:text>FAMILY</xsl:text>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="@Provider_ID">
        <xsl:attribute name="Provider_ID">
            <xsl:text>astral.com</xsl:text>
        </xsl:attribute>
    </xsl:template>
    <xsl:template
            match="@Product|@Asset_Name|@Version_Major|@Version_Minor|@Description|@Creation_Date|@Asset_ID|@Asset_Class">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>