XML上的XSLT从混合节点获取父值

时间:2019-07-19 02:01:17

标签: xslt-2.0

我有一个带有一些混合节点的XML,我想只获取父级而不是子级的值。

我的XML

<?xml version="1.0" encoding="UTF-8"?>
<Records>
   <DET>
      <detnumber>100126</detnumber>
      <EmployeeNo>100126</EmployeeNo>
      <action>CHANGE</action>
      <first_name> NewHire-4th
                        <previous>NewHire</previous>
                     </first_name>
      <last_name>Test-Changed 4th
                        <previous>Test-Changed 3rd</previous>
                     </last_name>
      <birth_name>
                        NewHire-Changed 4th
                        <previous>NewHire-Changed 3rd</previous>
                     </birth_name>
      <formal_name>
                        NewHire-4th Test-Changed 4th
                        <previous>NewHire Test-Changed 3rd</previous>
                     </formal_name>
      <salutation>
                        MISS
                        <previous>MRS</previous>
                     </salutation>
      <email_address>
                        testHire4@gmail.com
                        <previous>testHire2@gmail.com</previous>
                     </email_address>
   </DET>
</Records>

使用XSLT 2.0

我在xslt中主要使用的副本,但是整个Node及其子节点都被复制了。我只需要限制为父母。

<xsl:copy-of select="first_name"/>
<xsl:copy-of select="last_name"/>
<xsl:copy-of select="birth_name"/>
<xsl:copy-of select="formal_name"/>
<xsl:copy-of select="salutation"/>

以下是我的首选输出

<?xml version="1.0" encoding="UTF-8"?>
<Records>
   <DET>
      <detnumber>100126</detnumber>
      <EmployeeNo>100126</EmployeeNo>
      <action>CHANGE</action>
      <first_name> NewHire-4th</first_name>
      <last_name>Test-Changed 4th</last_name>
      <birth_name>NewHire-Changed 4th</birth_name>
      <formal_name>NewHire-4th Test-Changed 4th</formal_name>
      <salutation>MISS</salutation>
      <email_address>testHire4@gmail.com</email_address>
   </DET>
</Records>

1 个答案:

答案 0 :(得分:0)

Check this Code:-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

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

    <xsl:template match="previous"/>
</xsl:stylesheet>