2012-05-09 171 views
2

我有一個XML文件:如何從XML獲取XPath?

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <rows> 
     <column account="1" name="balibid" seq="1">1</column> 
     <column account="1" name="genre" seq="1">Kids</column> 
     <column account="1" name="ph" seq="1">3</column> 
     <column account="1" name="ph" seq="1">4</column> 
     <column account="1" name="ph" seq="1">5</column> 
     <column account="1" name="ph" seq="1">6</column> 
     <column account="1" name="pl6" seq="1">6</column> 
</rows> 
</response> 

我需要的列名=流派的XPath。我應該使用什麼?

回答

0

試試這個:

//column[@name='genre'] 
3

在XPath查詢中使用//導致不必要的(和潛在的昂貴)的評價,這是我如何開始(因爲它很容易),但我現在努力地踢習慣。

鑑於您提供了一個很好的,格式良好的樣本。這裏有兩個例子XPath查詢:

/response/rows/column[@name="genre"] 

而且

rows/column[@name="genre"] 
0

使用(針對特定的提供的XML文檔):

/*/*/*[@name = 'genre'] 

XSLT - 基於驗證:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <xsl:copy-of select="/*/*/*[@name = 'genre']"/> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔應用:

<response> 
    <rows> 
     <column account="1" name="balibid" seq="1">1</column> 
     <column account="1" name="genre" seq="1">Kids</column> 
     <column account="1" name="ph" seq="1">3</column> 
     <column account="1" name="ph" seq="1">4</column> 
     <column account="1" name="ph" seq="1">5</column> 
     <column account="1" name="ph" seq="1">6</column> 
     <column account="1" name="pl6" seq="1">6</column> 
</rows> 
</response> 

XPath表達式求值和所選擇的節點被複制到輸出:

<column account="1" name="genre" seq="1">Kids</column>