建议对来自两个XML的信息进行分组。在我的帖子中,基于员工转移和路线号,其他信息将被分组。查看所需的OutPut以便快速查看。 (XSLT 2)
输入XML(EmpShift.xml输入到XSLT):
<MPS>
<emp>
<name>Rudramuni TP</name>
<ID>MAC000424</ID>
<Shift>Second</Shift>
</emp>
<emp>
<name>Mohan</name>
<ID>MAC000425</ID>
<Shift>Second</Shift>
</emp>
<emp>
<name>Vijay</name>
<ID>MAC000426</ID>
<Shift>First</Shift>
</emp>
<emp>
<name>Shankar</name>
<ID>MAC000427</ID>
<Shift>First</Shift>
</emp>
<emp>
<name>Prasad</name>
<ID>MAC000428</ID>
<Shift>Second</Shift>
</emp>
</MPS>
第二个输入(称为XML EmpAddressInfo.xml):
<Addess_Info>
<emp>
<name>Rudramuni TP</name>
<ID>MAC000424</ID>
<address>Nandini Layout, B-96</address>
<Route_No>10</Route_No>
</emp>
<emp>
<name>Mohan</name>
<ID>MAC000425</ID>
<address>Banashankari</address>
<Route_No>11</Route_No>
</emp>
<emp>
<name>Vijay</name>
<ID>MAC000426</ID>
<address>Marathahalli</address>
<Route_No>10</Route_No>
</emp>
<emp>
<name>Shankar</name>
<ID>MAC000427</ID>
<address>Yelahanka</address>
<Route_No>11</Route_No>
</emp>
<emp>
<name>Prasad</name>
<ID>MAC000428</ID>
<address>Marathahalli</address>
<Route_No>10</Route_No>
</emp>
</Addess_Info>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="varDocAdress" select="document('EmpAddressInfo.xml')"/>
<xsl:template match="MPS">
<xsl:for-each-group select="//emp" group-by="Shift">
<!--xsl:for-each-group select="current-group()" group-by="$varDocAdress/Route_No"-->
<Shift>
<Shift-Name><xsl:value-of select="current-grouping-key()"/></Shift-Name>
<empDetails><xsl:apply-templates select="current-group()/name|current-group()/ID"/></empDetails>
</Shift>
<!--/xsl:for-each-group-->
</xsl:for-each-group>
</xsl:template>
<xsl:template match="name">
<name1><xsl:apply-templates/></name1>
</xsl:template>
<xsl:template match="ID">
<ID><xsl:apply-templates/></ID>
</xsl:template>
</xsl:stylesheet>
必填结果:
<MPS>
<Shift>
<Shift-Name>Second</Shift-Name>
<Route_No><title>10</title>
<empDetails>
<name1>Rudramuni TP</name1><ID>MAC000424</ID><address>Nandini Layout, B-96</address>
</empDetails>
<empDetails>
<name1>Prasad</name1><ID>MAC000428</ID><address>Marathahalli</address>
</empDetails>
</Route_No>
<Route_No><title>11</title>
<empDetails>
<name1>Mohan</name1><ID>MAC000425</ID><address>Banashankari</address>
</empDetails>
</Route_No>
</Shift>
<Shift>
<Shift-Name>First</Shift-Name>
<Route_No><title>10</title>
<empDetails>
<name1>Vijay</name1><ID>MAC000426</ID><address>Marathahalli</address>
</empDetails>
</Route_No>
<Route_No><title>11</title>
<empDetails>
<name1>Shankar</name1><ID>MAC000427</ID><address>Yelahanka</address>
</empDetails>
</Route_No>
</Shift>
</MPS>
答案 0 :(得分:1)
试试这个XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="varDocAdress" select="document('EmpAddressInfo.xml')"/>
<xsl:template match="MPS">
<xsl:copy>
<xsl:for-each-group select="//emp" group-by="Shift">
<Shift>
<Shift-Name>
<xsl:value-of select="current-grouping-key()"/>
</Shift-Name>
<xsl:for-each-group select="$varDocAdress/Addess_Info/emp[ID = current-group()/ID]" group-by="Route_No">
<Route_No>
<title><xsl:value-of select="current-grouping-key()"/></title>
<xsl:for-each select="current-group()">
<empDetails>
<xsl:apply-templates select="current()"/>
</empDetails>
</xsl:for-each>
</Route_No>
</xsl:for-each-group>
</Shift>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="emp">
<name1>
<xsl:value-of select="name"/>
</name1>
<xsl:copy-of select="ID | address"/>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
我会使用密钥进行交叉引用:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf"
version="2.0">
<xsl:param name="address-url" select="'test2014112603.xml'"/>
<xsl:variable name="address-doc" select="doc($address-url)"/>
<xsl:output indent="yes"/>
<xsl:key name="emp-by-id" match="emp" use="ID"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MPS">
<xsl:copy>
<xsl:for-each-group select="emp" group-by="Shift">
<Shift>
<Shift-Name><xsl:value-of select="current-grouping-key()"/></Shift-Name>
<xsl:for-each-group select="current-group()" group-by="key('emp-by-id', ID, $address-doc)/Route_No">
<Route_No>
<title><xsl:value-of select="current-grouping-key()"/></title>
<xsl:apply-templates select="current-group()"/>
</Route_No>
</xsl:for-each-group>
</Shift>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="emp">
<empDetails>
<xsl:apply-templates select="name, ID, key('emp-by-id', ID, $address-doc)/address"/>
</empDetails>
</xsl:template>
<xsl:template match="name">
<name1><xsl:apply-templates/></name1>
</xsl:template>
</xsl:stylesheet>
答案 2 :(得分:1)
我建议您使用键:
来简化此操作XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="varDocAdress" select="document('EmpAddressInfo.xml')"/>
<xsl:key name="emp-by-id" match="emp" use="ID" />
<xsl:template match="/MPS">
<xsl:for-each-group select="emp" group-by="Shift">
<Shift>
<Shift-Name><xsl:value-of select="current-grouping-key()"/></Shift-Name>
<xsl:for-each-group select="current-group()" group-by="key('emp-by-id', ID, $varDocAdress)/Route_No">
<Route_No>
<title><xsl:value-of select="current-grouping-key()"/></title>
<xsl:apply-templates select="current-group()"/>
</Route_No>
</xsl:for-each-group>
</Shift>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="emp">
<empDetails>
<xsl:copy-of select="name | ID | key('emp-by-id', ID, $varDocAdress)/address "/>
</empDetails>
</xsl:template>
</xsl:stylesheet>