我有这个xml文件....我必须检查SE / SSE的状态...如果它是活动的那么它将进入详细信息标签....它将读取状态...如果它是活动的,然后它将读取详细信息标记,除非它将丢弃该详细信息节点。就像那样,如果SSE状态是" InACTIVE",则无需在该节点内读取。
<Employees>
<Employee>
<SE>
<Name>bikash</Name>
<dept>DY</dept>
<status>ACTIVE</status>
<Details dataStr="list">
<status>ACTIVE</status>
<address>India</address>
<streetNo>19</streetNo>
</Details>
<Details dataStr="list">
<status>InACTIVE</status>
<address>CHINA</address>
<streetNo>20</streetNo>
</Details>
<area>BLORE</area>
<SEIdCount>1</SEIdCount>
</SE>
<SSE>
<status>InACTIVE</status>
<emplNo>23</emplNo>
<Details dataStr="list">
<status>InActive</status>
<absent>y</absent>
</Details>
<Details dataStr="list">
<status>Active</status>
<name>anu</NAME>
</Details>
<area>CHN</area>
<SEIdCount>2</SEIdCount>
</SSE>
</Employee>
</Employees>
我的预期回应是这个
<Employees>
<Employee>
<SE>
<Name>bikash</Name>
<dept>DY</dept>
<status>ACTIVE</status>
<Details dataStr="list">
<status>ACTIVE</status>
<address>India</address>
<streetNo>19</streetNo>
</Details>
<area>BLORE</area>
<SEIdCount>1</SEIdCount>
</SE>
</Employee>
</Employees>
答案 0 :(得分:1)
尝试:
XSLT 1.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="/Employees">
<Employees>
<xsl:for-each select="Employee/SE">
<Employee>
<SE>
<xsl:copy-of select="Name | dept | status | Details[status='Active'] | area | SEIdCount"/>
</SE>
</Employee>
</xsl:for-each>
</Employees>
</xsl:template>
</xsl:stylesheet>
应用于格式良好的XML输入:
<Employees>
<Employee>
<SE>
<Name>bikash</Name>
<dept>DY</dept>
<status>Active</status>
<Details dataStr="list">
<status>Active</status>
<address>India</address>
<streetNo>19</streetNo>
</Details>
<Details dataStr="list">
<status>InACTIVE</status>
<address>CHINA</address>
<streetNo>20</streetNo>
</Details>
<area>BLORE</area>
<SEIdCount>1</SEIdCount>
</SE>
<SSE>
<status>InACTIVE</status>
<emplNo>23</emplNo>
<Details dataStr="list">
<status>InActive</status>
<absent>y</absent>
</Details>
<Details dataStr="list">
<status>Active</status>
<name>anu</name>
</Details>
<area>CHN</area>
<SEIdCount>2</SEIdCount>
</SSE>
</Employee>
</Employees>
这将返回:
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee>
<SE>
<Name>bikash</Name>
<dept>DY</dept>
<status>Active</status>
<Details dataStr="list">
<status>Active</status>
<address>India</address>
<streetNo>19</streetNo>
</Details>
<area>BLORE</area>
<SEIdCount>1</SEIdCount>
</SE>
</Employee>
</Employees>
select="Name"
不选择<name>
,&#34;有效&#34;是不与&#34; ACTIVE&#34;。
答案 1 :(得分:-1)
使用XSLT 1.0 如果您没有SE节点中的数字子节点已修复
,您也可以尝试这一个 <xsl:template match="/*/Employee">
<Employees>
<Employee>
<SE>
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="status='ACTIVE'">
<xsl:for-each select="./*">
<xsl:choose>
<xsl:when test="local-name(.)='Details' and ./status='ACTIVE'">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="local-name(.)='Details' and ./status='InACTIVE'"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</SE>
</Employee>
</Employees>
</xsl:template>