使用XSLT向下移动节点

时间:2014-10-08 22:13:07

标签: xml xslt

在我提出问题之后,我设法将节点向上移动(Moving nodes up using XSLT)。在此之后,我以为我明白了,所以我尝试了相反的方法来移动一个节点。没有工作。这就是我所做的:

我的输入数据名为DEBTORS.xml:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 001" status="A" type="C">
  <Name>Name</Name>
  <Contacts>
   <Contact default="1" gender="M" status="A">
    <Note>Patient: 1</Note>
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的XSL名为Test2.xsl

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Indentation in XSL -->
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

<!-- Removing blank lines in XSL -->
<xsl:strip-space  elements="*"/>

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

<!-- special rules ... -->
    <xsl:template match="Account">
        <xsl:copy>
          <!-- exclude Name -->
          <xsl:apply-templates select="@* | node()[not(self::Name)]"/>
        </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

通缉输出:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 001" status="A" type="C">
  <Contacts>
   <Contact default="1" gender="M" status="A">
    <Name>Name</Name>
    <Note>Patient: 1</Note>
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的问题是我的XSL节点&#34; Name&#34;被删除,但作为联系的孩子不会回来。希望有人能帮助我?

1 个答案:

答案 0 :(得分:1)

我建议您进行一些更改:

  1. 要取消Name,请添加与之匹配的模板规则 什么都没有。
  2. 要将Name添加到Contact,请添加与Contact匹配的模板规则 像往常一样复制,但也会插入Name
  3. 删除当前与Contacts匹配的模板(复数);该 一般身份规则可以处理那么好。
  4. 以下是您完整更新的样式表:

    <xsl:stylesheet version="1.0" 
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <!-- Indentation in XSL -->
      <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"
                  encoding="UTF-8" indent="yes"/>
    
      <!-- Removing blank lines in XSL -->
      <xsl:strip-space  elements="*"/>
    
      <!-- Identity rule -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <!-- special rules ... -->
      <xsl:template match="Name"/>
    
      <xsl:template match="Contact">
        <xsl:copy>
          <!-- include Name -->
          <xsl:apply-templates select="@*"/>
          <Name><xsl:value-of select="../../Name"/></Name>
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    根据您的示例输入XML,上面的XSLT生成请求的输出XML:

    <eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
       <Accounts>
          <Account code="                 001" status="A" type="C">
             <Contacts>
                <Contact default="1" gender="M" status="A">
                   <Name>Name</Name>
                   <Note>Patient: 1</Note>
                   <FirstName/>
                   <Addresses>
                      <Address type="D" desc="">
                         <AddressLine1>Street</AddressLine1>
                         <AddressLine2/>
                         <AddressLine3/>
                         <PostalCode>0000 AA</PostalCode>
                         <City>'City</City>
                         <Country code="NL"/>
                         <Phone/>
                         <Fax/>
                      </Address>
                   </Addresses>
                   <Language code="NL"/>
                   <JobDescription>--</JobDescription>
                   <Phone/>
                   <PhoneExt/>
                   <Fax/>
                   <Mobile/>
                   <Email/>
                   <WebAccess>0</WebAccess>
                </Contact>
             </Contacts>
             <Debtor number="   1" code="                 1">
                <Currency code="EUR"/>
             </Debtor>
          </Account>
       </Accounts>
    </eExact>