2017-03-03 74 views
0

我有一個這樣的XML文件追加一個XML節點到另一個XML:如何使用XPath

<?xml version="1.0" encoding="UTF-8"?> 
<Rowsets> 
    <Rowset> 
     <Row> 
      <Product>Product Name 1</Product> 
      <EmployeeID>12345</EmployeeID> 
      <SampleID>1</SampleID> 
      <LineNo>Line1</LineNo> 
     </Row> 
     <Row> 
      <Product>Product Name 2</Product> 
      <EmployeeID>123456</EmployeeID> 
      <SampleID>2</SampleID> 
      <LineNo>Line2</LineNo> 
     </Row> 
     <Row> 
      <Product>Product Name 3</Product> 
      <EmployeeID>1234567</EmployeeID> 
      <SampleID>3</SampleID> 
      <LineNo>Line3</LineNo> 
     </Row> 
    </Rowset> 
</Rowsets> 

和像另一個XML文件:現在

<Rowsets> 
    <Rowset> 
     <Row> 
      <Prod_Shift>1</Prod_Shift> 
      <Target>2</Target> 
      <FileName>FileName</FileName> 
     </Row> 
    </Rowset> 
</Rowsets> 

,對於每一個 「行」第一個XML中的節點,我需要附加第二個XML的內容。

和追加第一第二XML後,第一個XML應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<Rowsets> 
    <Rowset> 
     <Row> 
      <Product>Product Name 1</Product> 
      <EmployeeID>12345</EmployeeID> 
      <SampleID>1</SampleID> 
      <LineNo>Line1</LineNo> 
      <Prod_Shift>1</Prod_Shift> 
      <Target>2</Target> 
      <FileName>FileName</FileName> 
     </Row> 
     <Row> 
      <Product>Product Name 2</Product> 
      <EmployeeID>123456</EmployeeID> 
      <SampleID>2</SampleID> 
      <LineNo>Line2</LineNo> 
      <Prod_Shift>1</Prod_Shift> 
      <Target>2</Target> 
      <FileName>FileName</FileName> 
     </Row> 
     <Row> 
      <Product>Product Name 3</Product> 
      <EmployeeID>1234567</EmployeeID> 
      <SampleID>3</SampleID> 
      <LineNo>Line3</LineNo> 
      <Prod_Shift>1</Prod_Shift> 
      <Target>2</Target> 
      <FileName>FileName</FileName> 
     </Row> 
    </Rowset> 
</Rowsets> 

我是新來的XPath。 任何幫助,將不勝感激。

+1

的XPath無法單獨做到這一點,你需要使用XSLT或XQuery或喜歡使用XPath C#,PHP,Python和Java的暴露DOM操作在一起的任何語言。 –

回答

0

您需要XSLT:

<xsl:variable name="extra" select="document('doc2.xml')//Row/node()"/> 

<xsl:template match="/"> 
    <Rowsets> 
    <Rowset> 
     <xsl:apply-templates select="//Row"/> 
    </Rowset> 
    </Rowsets> 
</xsl:template> 

<xsl:template match="Row"> 
    <row> 
    <xsl:copy-of select="node()"/> 
    <xsl:copy-of select="$extra"/> 
    </row> 
</xsl:template>