Xml 对于XSLT中的每个循环
我想使用XSLT进行一次转换。根据下面的示例,有多个EMP记录可用,每个EMP记录包含EmpDept和该部门下可用员工的档案 XML:Xml 对于XSLT中的每个循环,xml,xslt,Xml,Xslt,我想使用XSLT进行一次转换。根据下面的示例,有多个EMP记录可用,每个EMP记录包含EmpDept和该部门下可用员工的档案 XML: <Employee> <EmpRecord> <EmpDept>Accounting</EmpDept> <EmpData> <Name>Joy</Name> <Age>32<
<Employee>
<EmpRecord>
<EmpDept>Accounting</EmpDept>
<EmpData>
<Name>Joy</Name>
<Age>32</Age>
</EmpData>
</EmpRecord>
<EmpRecord>
<EmpDept>Finance</EmpDept>
</EmpRecord>
<EmpRecord>
<EmpDept>IT</EmpDept>
<EmpData>
<Name>Sam</Name>
<Age>27</Age>
</EmpData>
<EmpData>
<Name>John</Name>
<Age>25</Age>
</EmpData>
<EmpData>
<Name>Ricky</Name>
<Age>31</Age>
</EmpData>
</EmpRecord>
</Employee>
会计
快乐
32
财务
信息技术
山姆
27
约翰
25
瑞奇
31
预期输出:
<Employee>
<EmpRecord>
<Department>Accounting</Department>
<EmpData>
<EmpName>Joy</EmpName>
<Age>32</Age>
</EmpData>
</EmpRecord>
<EmpRecord>
<Department>IT</Department>
<EmpData>
<EmpName>Sam</EmpName>
<Age>27</Age>
</EmpData>
<EmpData>
<EmpName>John</EmpName>
<Age>25</Age>
</EmpData>
<EmpData>
<EmpName>Ricky</EmpName>
<Age>31</Age>
</EmpData>
</EmpRecord>
</Employee>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Employee>
<xsl:for-each select="/Employee/EmpRecord/EmpData">
<Department><xsl:value-of select="./EmpDept"/></Department>
<EmpData>
<EmpName><xsl:value-of select="./Name"/></EmpName>
<Age><xsl:value-of select="./Age"/></Age>
</EmpData>
</xsl:for-each>
</Employee>
</xsl:template>
</xsl:stylesheet>
会计
快乐
32
信息技术
山姆
27
约翰
25
瑞奇
31
我能够获取所有可用的EmpData,但无法获取EmpDept
使用的XSLT:
<Employee>
<EmpRecord>
<Department>Accounting</Department>
<EmpData>
<EmpName>Joy</EmpName>
<Age>32</Age>
</EmpData>
</EmpRecord>
<EmpRecord>
<Department>IT</Department>
<EmpData>
<EmpName>Sam</EmpName>
<Age>27</Age>
</EmpData>
<EmpData>
<EmpName>John</EmpName>
<Age>25</Age>
</EmpData>
<EmpData>
<EmpName>Ricky</EmpName>
<Age>31</Age>
</EmpData>
</EmpRecord>
</Employee>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Employee>
<xsl:for-each select="/Employee/EmpRecord/EmpData">
<Department><xsl:value-of select="./EmpDept"/></Department>
<EmpData>
<EmpName><xsl:value-of select="./Name"/></EmpName>
<Age><xsl:value-of select="./Age"/></Age>
</EmpData>
</xsl:for-each>
</Employee>
</xsl:template>
</xsl:stylesheet>
因此,我只想要那些至少有一名员工可用的EmpDept。您实际想要做的是:将所有内容从源xml复制到目标xml,除了
EmpRecord
不包含任何EmpData
。对吗?试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/>
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- specific part -->
<xsl:template match="EmpRecord[not(EmpData)]"/>
</xsl:stylesheet>
“identity template”复制所有内容,“specific part”删除不需要的元素。您真正想做的是:将所有内容从源xml复制到目标xml,除了
EmpRecord
不包含任何EmpData
。对吗?试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/>
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- specific part -->
<xsl:template match="EmpRecord[not(EmpData)]"/>
</xsl:stylesheet>
“标识模板”复制所有内容,“特定部分”删除不需要的元素。请尝试以下方法:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Employee">
<Employee>
<xsl:for-each select="EmpRecord[EmpData]">
<EmpRecord>
<Department>
<xsl:value-of select="EmpDept"/>
</Department>
<xsl:for-each select="EmpData">
<EmpData>
<EmpName>
<xsl:value-of select="Name"/>
</EmpName>
<xsl:copy-of select="Age"/>
</EmpData>
</xsl:for-each>
</EmpRecord>
</xsl:for-each>
</Employee>
</xsl:template>
</xsl:stylesheet>
或者,如果您愿意:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="EmpRecord[not(EmpData)]"/>
<xsl:template match="EmpDept">
<Department>
<xsl:apply-templates/>
</Department>
</xsl:template>
<xsl:template match="Name">
<EmpName>
<xsl:apply-templates/>
</EmpName>
</xsl:template>
</xsl:stylesheet>
试着这样做:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Employee">
<Employee>
<xsl:for-each select="EmpRecord[EmpData]">
<EmpRecord>
<Department>
<xsl:value-of select="EmpDept"/>
</Department>
<xsl:for-each select="EmpData">
<EmpData>
<EmpName>
<xsl:value-of select="Name"/>
</EmpName>
<xsl:copy-of select="Age"/>
</EmpData>
</xsl:for-each>
</EmpRecord>
</xsl:for-each>
</Employee>
</xsl:template>
</xsl:stylesheet>
或者,如果您愿意:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="EmpRecord[not(EmpData)]"/>
<xsl:template match="EmpDept">
<Department>
<xsl:apply-templates/>
</Department>
</xsl:template>
<xsl:template match="Name">
<EmpName>
<xsl:apply-templates/>
</EmpName>
</xsl:template>
</xsl:stylesheet>
很抱歉,如果我的预期输出有误导性,实际上我不想完全复制。在我的实际工作中需要更改标记和结构。如果我的预期输出有误导性,很抱歉,实际上我不想完全复制。在我的实际工作中,需要更改标签和结构。