如何使用XSLT从XML中删除特定元素

时间:2015-08-11 08:45:17

标签: xml xslt

我有这样的XML:

<?xml version="1.0" encoding="utf-8"?>
<batch xmlns="http://www.concursolutions.com/api/user/2011/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <UserProfile>
    <EmpId>123456</EmpId>
    <NewEmployeeID>
    </NewEmployeeID>
  </UserProfile>
</batch>

我需要将此XML转换为此形状:

<?xml version="1.0" encoding="utf-8"?>
<batch xmlns="http://www.concursolutions.com/api/user/2011/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <UserProfile>
    <EmpId>123456</EmpId>
  </UserProfile>
</batch>

这是我试过的XSLT,但它没有工作:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
>

  <xsl:output method="xml"
              indent="yes"/>

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

  <xsl:template match="NewEmployeeID"/>

</xsl:stylesheet>

你能帮忙吗?

非常感谢

1 个答案:

答案 0 :(得分:2)

NewEmployeeID位于默认命名空间中,其中uri为"http://www.concursolutions.com/api/user/2011/02"。您需要使用映射到默认命名空间uri的前缀来匹配该元素:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet .......
       xmlns:d="http://www.concursolutions.com/api/user/2011/02">
  <xsl:strip-space elements="*"/>
  .........
  <xsl:template match="d:NewEmployeeID"/>
</xsl:stylesheet>