在PHP放棄我試圖使用XSLTXSLT的選擇命名空間屬性
到提取XML數據後怎樣在<jskit:attribute key="permalink"
數據符合我看到了有關名稱值對另一個類似的問題但林一步之遙從
我的XML在這裏給出xml source
在PHP放棄我試圖使用XSLTXSLT的選擇命名空間屬性
到提取XML數據後怎樣在<jskit:attribute key="permalink"
數據符合我看到了有關名稱值對另一個類似的問題但林一步之遙從
我的XML在這裏給出xml source
首先處理指令,請確保您要使用的所有命名空間都可以在xslt中。例如,定義的xmlns前綴爲所有這些在頂部元件,像這樣:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jskit="http://purl.org/dc/elements/1.1/"
>
... xslt templates go here...
</xsl:stylesheet>
之後,你可以在你的XPath表達式使用命名空間前綴jskit。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jskit="http://purl.org/dc/elements/1.1/"
>
<!-- match all items anywhere in the document -->
<xsl:template match="//item">
<!-- get the "value" attribute of the "jskit:attribute" element
in the current item in case the "key" attribute is called "permalink"
-->
<xsl:value-of select="jskit:attribute[@key='permalink']/@value"/>
, <!-- comma separator, literal txt -->
<!-- get the "value" attribute of the "jskit:attribute" element
in the current item in case the "key" attribute is called "IP"
-->
<xsl:value-of select="jskit:attribute[@key='IP']/@value"/>
</xsl:template>
</xsl:stylesheet>
你需要寫的模板和XPath完全依賴於你所需的輸出格式的要求,這是不容易完全回答,如果我們缺乏載文信息
但是,假設你想將數據插入到數據庫中,然後你可以直接用XSLT生成SQL語句:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jskit="http://purl.org/dc/elements/1.1/"
>
<!-- make text output (SQL script) -->
<xsl:output
method="text"
/>
<!-- match all items anywhere in the document -->
<xsl:template match="//item">
INSERT INTO myTable(permalink, IP) VALUES
('<xsl:value-of select="jskit:attribute[@key='permalink']/@value"/>'
,'<xsl:value-of select="jskit:attribute[@key='IP']/@value"/>');
</xsl:template>
</xsl:stylesheet>
但是,爲了使這種事情真的很強大的,你必須確保在SQL語句最終值不包含任何字符串de限制器。例如,如果永久鏈接可能包含單引號,那麼這將生成無效的SQL,因爲來自該值的單引號會過早地結束字符串文字SQL值。
爲了應對這一點,你可以寫一個遞歸處理文本值逃避那些需要它(除了報價,並根據您的數據庫,你可能需要太逃避其他字符)字符的模板
Anothter方法是使用XSLT將數據轉換爲您可以輕鬆解析的主機語言格式。假設您正在使用PHP,那麼您可以使用XSLT將XML轉換爲ini文件格式,使用parse_ini_file()解析該文件,然後使用PHP正確地轉義/驗證值並執行數據庫操作。
因人而異
有問題的節點的XPath表達式如下:
/rss/channel/item/jskit:attribute[@key='IP']/@value
/rss/channel/item/jskit:attribute[@key='permalink']/@value
/rss/channel/item/jskit:parent-guid
前兩個使用謂詞來選擇具有等於'IP'或'永久鏈接'的對應關鍵字的節點。第三個是選擇單個節點的正常表達式。
爲了能夠使用名稱空間訪問這些節點,您需要在XSLT中定義名稱空間前綴。這在<xsl:stylesheet>
元素中完成。對於這個應用程序,您可以從XML源複製聲明。
在XSLT完整的例子
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:jskit="http://purl.org/dc/elements/1.1/"
exclude-result-prefixes="atom media jskit dc">
<xsl:template match="/">
<ip>
<xsl:value-of
select="/rss/channel/item/jskit:attribute[@key='IP']/@value"/>
</ip>
<permalink>
<xsl:value-of
select="/rss/channel/item/jskit:attribute[@key='permalink']/@value"/>
</permalink>
<parent-guid>
<xsl:value-of
select="/rss/channel/item/jskit:parent-guid"/>
</parent-guid>
</xsl:template>
</xsl:stylesheet>
將樣式表附加到XML文件中使用名爲xml-stylesheet
<?xml version="1.0"?>
<?xml-stylesheet href="rss.xsl" type="text/xsl" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:jskit="http://purl.org/dc/elements/1.1/" >
在PHP放棄後,我試圖使用XSLT
什麼是放棄以提取XML數據? PHP對XML有很好的支持。您可以使用DomXPath
在xml文檔中進行查詢。首先使用registerNamespace
,以便能夠查詢命名空間屬性。例如:
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("jskit", "http://purl.org/dc/elements/1.1/");
$query = "//jskit:attribute[@key='permalink']";
foreach ($xpath->evaluate($query) as $node) {
echo $node, "\n";
}
這個答案將是完美的原始問題http://stackoverflow.com/questions/1977072/rss-xml-namespace- confusion – 2009-12-30 09:37:32
@peter .. THX這麼多的完整的例子 如果我附上XSL到XML我得到的錯誤在第1行 誤差在列25:額外的內容,在結束文檔 – vk123 2009-12-30 09:10:12
xslt進入.xsl文件並將xml轉換爲.xml文件。爲了將xslt附加到xml中,您需要使用類似於<?xml-stylesheet href =「rss.xsl」type =「text/xsl」?> – 2009-12-30 09:21:06
感謝Peter和Roland!終於找到工作了! – vk123 2009-12-30 09:45:49