3

我需要使用 XPATH 和 XSLT 以某种特定方式从 XML 中提取数据

<data>      
    <person id="p1">         
        <name>User1</name>      
    </person>   
    <person id="p2">         
        <name>User2</name>      
    </person>  
    <person id="p3">         
        <name>User3</name>      
    </person>       
    <employee eid="emp1" pid="p1">         
        <dept>dept1</dept>      
    </employee> 
    <employee eid="emp2" pid="p3">         
        <dept>dept3</dept>      
    </employee>
    <employee eid="emp3" pid="p2">         
        <dept>dept1</dept>      
    </employee>
</data>

从上面的示例中,我需要在输出 xml 中使用 Person 和相应的 Employee 元素创建每个 XML。这两个xml之间的链接是

person.id = 员工.pid

像 XML1:

<person id="p1">         
<name>User1</name>      
</person>  
<employee eid="emp1" pid="p1">         
<dept>dept1</dept>      
</employee>

XML2:

<person id="p2">         
<name>User2</name>      
</person> 
<employee eid="emp3" pid="p2">         
 <dept>dept1</dept>      
</employee>

XML3:

<person id="p3">         
<name>User3</name>      
</person> 
<employee eid="emp2" pid="p3">         
<dept>dept3</dept>      
</employee>  

我尝试了很多方法,但无法做到这一点。

谢谢...

4

2 回答 2

2

XPath 是一种用于 XML 文档的查询语言——因此 XPath 表达式的求值不能修改现有文档或创建新的 XML 文档

使用 XSLT 2.0 可以最好地实现您想要的(如果只需要一个结果文档,则使用 XSLT 1.0):

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

    <xsl:key name="kEmpByPid" match="employee" use="@pid"/>

 <xsl:template match="person">
  <xsl:result-document href="file:///c:/temp/delete/XML{position()}">
      <t>
             <xsl:copy-of select=".|key('kEmpByPid', @id)"/>
         </t>
     </xsl:result-document>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此 XSLT 2.0 转换应用于提供的 XML 文档时:

<data>
    <person id="p1">
        <name>User1</name>
    </person>
    <person id="p2">
        <name>User2</name>
    </person>
    <person id="p3">
        <name>User3</name>
    </person>
    <employee eid="emp1" pid="p1">
        <dept>dept1</dept>
    </employee>
    <employee eid="emp2" pid="p3">
        <dept>dept3</dept>
    </employee>
    <employee eid="emp3" pid="p2">
        <dept>dept1</dept>
    </employee>
</data>

在“c:\temp\delete”中创建了以下三个 XML 文档:

XML1

<t>
   <person id="p1">
      <name>User1</name>
   </person>
   <employee eid="emp1" pid="p1">
      <dept>dept1</dept>
   </employee>
</t>

XML2

<t>
   <person id="p2">
      <name>User2</name>
   </person>
   <employee eid="emp3" pid="p2">
      <dept>dept1</dept>
   </employee>
</t>

XML3

<t>
   <person id="p3">
      <name>User3</name>
   </person>
   <employee eid="emp2" pid="p3">
      <dept>dept3</dept>
   </employee>
</t>
于 2012-10-19T18:44:25.467 回答
0

对于上面的问题,我有以下解决方案。

<xsl:stylesheet version="2.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="//person">
        <xsl:variable name="ID" select="@id" />
        <xsl:result-document href="file:///c:/temp/delete/XML{position()}">
        <t>
            <xsl:copy-of select=". | //employee[@pid=$ID]" />
        </t>                
        </xsl:result-document>
    </xsl:template>
    <xsl:template match="text()" />
</xsl:stylesheet>

只是想和你分享。

谢谢。

于 2012-10-23T19:55:16.777 回答