如何复制父节点以及具有特定属性的父节点的所有子节点应同时存在于父节点和子节点中



我有这个XML文件....我必须检查 SE/SSE 的状态...如果它处于活动状态,那么它将进入详细信息标签。它将读取状态...如果它处于活动状态,那么它将读取详细信息标记,除非它将丢弃该详细信息节点。就像这样,如果 SSE 状态为"非活动",则无需在该节点内部读取。

<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>

尝试:

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>

注意:XML 区分大小写:

select="Name"不会选择<name>,并且"活动"与"活动"不是一回事。

使用 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>

相关内容

  • 没有找到相关文章