使用调用另一个XML文档的XSL转换XML

时间:2018-04-03 21:10:57

标签: xml xslt document

我正在尝试从另一个XML文档动态地将一个Employee ID列表添加到一个XML文档中。本质上,我正在尝试构建XML查询并从另一个XML结果文档中插入Employees列表。

这是XSL调用的EmployeeList.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ExportTXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:e="http://www.taleo.com/ws/so800/2009/01" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <e:User>
        <e:EmployeeID>0000000123</e:EmployeeID>
        <e:SystemUser>false</e:SystemUser>
    </e:User>
    <e:User>
        <e:EmployeeID>0000000224</e:EmployeeID>
        <e:SystemUser>false</e:SystemUser>
    </e:User>
</ExportTXML>

这是正在转换的XML:

<?xml version="1.0" encoding="UTF-8"?>
<quer:query productCode="SO1015" model="http://www.taleo.com/ws/so800/2009/01" projectedClass="User" locale="en" mode="T-XML" largegraph="true" preventDuplicates="false" xmlns:quer="http://www.taleo.com/ws/integration/query"><quer:subQueries/><quer:projections><quer:projection><quer:field path="EmployeeID"/></quer:projection></quer:projections><quer:projectionFilterings/><quer:filterings><quer:filtering>
  <quer:includedIn>
    <quer:field path="EmployeeID"/>
    <quer:list>
      <!--List will be filled at runtime.-->
    </quer:list>
  </quer:includedIn>
</quer:filtering></quer:filterings><quer:sortings/><quer:sortingFilterings/><quer:groupings/><quer:joinings/></quer:query>

以下是转换XMl并调用EmployeeList.xml的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:quer="http://www.taleo.com/ws/integration/query" xmlns:e="http://www.taleo.com/ws/so800/2009/01" exclude-result-prefixes="e">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:param name="EMPLOYEE_FILTER">C:\temp\EmployeeList.xml</xsl:param>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>

    <xsl:template match="/quer:query/quer:filterings/quer:filtering/quer:includedIn[quer:field[@path='EmployeeID']]/quer:list">
        <xsl:copy>
            <xsl:for-each select="document(replace($EMPLOYEE_FILTER, '\\', '/'))//e:User/e:EmployeeID">
                <quer:string>
                    <xsl:value-of select="."/>
                </quer:string>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<quer:query productCode="SO1015" model="http://www.taleo.com/ws/so800/2009/01" projectedClass="User" locale="en" mode="T-XML" largegraph="true" preventDuplicates="false" xmlns:quer="http://www.taleo.com/ws/integration/query"><quer:subQueries/><quer:projections><quer:projection><quer:field path="EmployeeID"/></quer:projection></quer:projections><quer:projectionFilterings/><quer:filterings><quer:filtering>
  <quer:includedIn>
    <quer:field path="EmployeeID"/>
    <quer:list>
        <quer:string>0000000123</quer:string>
        <quer:string>0000000224</quer:string>
    </quer:list>
  </quer:includedIn>
</quer:filtering></quer:filterings><quer:sortings/><quer:sortingFilterings/><quer:groupings/><quer:joinings/></quer:query>

1 个答案:

答案 0 :(得分:0)

您似乎想构建一个file网址,因此您需要例如document(concat('file:///', replace($EMPLOYEE_FILTER, '\\', '/')))///e:User/e:EmployeeID。如果辅助文档中的输入元素位于命名空间http://www.taleo.com/ws/integration/toolkit/2005/07中,则需要声明xmlns:e="http://www.taleo.com/ws/integration/toolkit/2005/07"