[英]How do I match SQL table rows with XML namespace elements?
I've successfully saved down this XML file to my server using the following PHP: 我已使用以下PHP成功将此 XML文件保存到服务器中:
file_put_contents("test.xml", fopen("http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015", 'r'));
Now i'm trying to get it into my DB. 现在,我正在尝试将其放入数据库。 Just trying to get it to work by executing SQL in the phpMyAdmin GUI.
只是试图通过在phpMyAdmin GUI中执行SQL使其正常工作。 I successfully setup the following table:
我成功设置了下表:
CREATE TABLE `test` (
`NEW_DATE` varchar(40) NOT NULL,
`BC_1MONTH` int(11) NULL,
`BC_3MONTH` int(11) NULL,
PRIMARY KEY (`NEW_DATE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The following code runs without error, but just results in the addition of one empty row to my DB. 以下代码运行无错误,但仅导致数据库中增加了一个空行。
LOAD XML LOCAL INFILE 'test.xml'
INTO TABLE test
ROWS IDENTIFIED BY '<content>';
I see here that MySQL server looks for field names matching the column names of the target table. 我在这里看到MySQL服务器正在寻找与目标表的列名匹配的字段名。 It is also mentioned that there is no requirement for every field in the XML file be matched with a column in the corresponding table.
还提到了并不需要将XML文件中的每个字段都与对应表中的列进行匹配。 Fields which have no corresponding columns are skipped.
没有相应列的字段将被跳过。 Are my column names not matching the XML because of the XML prefixes and namespaces or am I barking up the wrong tree?
是因为XML前缀和名称空间导致我的列名与XML不匹配,还是我在错误的树上吠叫?
This question shows how to reference the same XML elements using PHP. 该问题说明如何使用PHP引用相同的XML元素。 Apparently prefix definition is required using
registerXPathNamespace()
in order to build an Xpath: 显然,使用
registerXPathNamespace()
需要前缀定义以构建Xpath:
Do I need to build such an Xpath in SQL? 我是否需要在SQL中构建这样的Xpath?
Perhaps I could remove the namespace/prefix data in PHP when I save the XML down using something like LIBXML_NSCLEAN ; 当我使用LIBXML_NSCLEAN之类的东西将XML保存下来时,也许可以在PHP中删除名称空间/前缀数据; apparently this removes redundant namespace declarations.
显然,这删除了多余的名称空间声明。 What qualifies as redundant?
什么才算多余?
Another option for removing namespaces seems to be XSL stylesheets. 删除名称空间的另一个选择似乎是XSL样式表。 Per this question.
根据这个问题。
What is the best approach to solving this issue? 解决此问题的最佳方法是什么?
Essentially, your XML is too complex (nested nodes, attributes, namespaces) to readily import into MySQL using LOAD XML
. 本质上,您的XML太复杂(嵌套节点,属性,名称空间),因此无法使用
LOAD XML
轻松导入MySQL。 As your above link shows the statement supports only three different formats: 如您上面的链接所示,该语句仅支持三种不同的格式:
<row column1="value1" column2="value2" .../>
<row>
<column1>value1</column1>
<column2>value2</column2>
</row>
<row>
<field name='column1'>value1</field>
<field name='column2'>value2</field>
</row>
Therefore, you need to transform your raw XML into such format above aligned of course to your table's fields. 因此,您需要将原始XML转换成上述格式,当然要与表格的字段对齐。 Using XSLT can help tremendously.
使用XSLT可以极大地帮助您。 As information, XSLT is a special programming language that re-structures XML content to various forms for end-use needs.
作为信息,XSLT是一种特殊的编程语言,可以将XML内容重新组织为各种形式,以满足最终用途的需求。 Like other general purpose languages including Java, C#, Python, and VB, PHP comes equipped with an XSLT 1.0 processor.
与其他通用语言(包括Java,C#,Python和VB)一样,PHP配备了XSLT 1.0处理器。 You may need to enable the extension.
您可能需要启用扩展。
XSLT Script (save as .xsl or .xslt file) XSLT脚本(另存为.xsl或.xslt文件)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:doc="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
exclude-result-prefixes="doc m d">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<!-- PARSING NEEDED CONTENT -->
<xsl:template match="doc:entry">
<row>
<NEW_DATE><xsl:value-of select="doc:content/m:properties/d:NEW_DATE"/></NEW_DATE>
<BC_1MONTH><xsl:value-of select="doc:content/m:properties/d:BC_1MONTH"/></BC_1MONTH>
<BC_3MONTH><xsl:value-of select="doc:content/m:properties/d:BC_3MONTH"/></BC_3MONTH>
</row>
</xsl:template>
<!-- REMOVE UNNEEDED NODES -->
<xsl:template match="doc:title|doc:id|doc:update|doc:link|doc:updated"/>
</xsl:stylesheet>
PHP Script (loading and processing XML and XSL content) PHP脚本(加载和处理XML和XSL内容)
$doc = new DOMDocument();
// PARSING DIRECTLY FROM WEB PAGE
$doc->load('http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015');
$xsl = new DOMDocument;
$xsl->load('XSLTScript.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// Transform XML source
$newXml = $proc->transformToXML($doc);
// Save output to file
$xmlfile = 'Output.xml';
file_put_contents($xmlfile, $newXml);
XML Output (now XML content can be used in LOAD XML to import into MySQL) XML输出(现在XML内容可以在LOAD XML中使用,以导入到MySQL中)
<?xml version="1.0" encoding="UTF-8"?>
<pre>
<row>
<NEW_DATE>2015-01-02T00:00:00</NEW_DATE>
<BC_1MONTH>0.02</BC_1MONTH>
<BC_3MONTH>0.02</BC_3MONTH>
</row>
<row>
<NEW_DATE>2015-01-05T00:00:00</NEW_DATE>
<BC_1MONTH>0.02</BC_1MONTH>
<BC_3MONTH>0.03</BC_3MONTH>
</row>
<row>
<NEW_DATE>2015-01-06T00:00:00</NEW_DATE>
<BC_1MONTH>0.02</BC_1MONTH>
<BC_3MONTH>0.03</BC_3MONTH>
</row>
...
</pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.