2014-09-30 95 views
2

我已經完成了大量搜索,但我無法弄清楚如何準確使用模板。將節點向上移動使用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稱爲Test.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<!-- Indentation in XSL --> 
<xsl:output omit-xml-declaration="yes" 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="Contact"> 
     <xsl:copy> 
       <!-- 
       Apply the attributes of the current node and the attributes of all 
       childs 
       --> 
       <xsl:apply-templates select="@* | child::node()[not(self::Note)]"/> 
     </xsl:copy> 
    <xsl:apply-templates select="Note"/> 
</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="     868" status="A" type="C"> 
    <Name>Name</Name> 
    <Contacts> 
    <Contact default="1" gender="M" status="A"> 
    <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> 
    <Note>Patient: 1</Note> 
    <Debtor number=" 1" code="     1"> 
    <Currency code="EUR"/> 
    </Debtor> 
    </Account> 
</Accounts> 
</eExact> 

我的問題是,隨着我的XSL節點「注意「作爲聯繫人的孩子來了,但我想要它作爲帳戶的孩子。希望有人能幫助我?

回答

3

我的問題是,與我的XSL節點「注」來作爲 聯繫人的孩子,但我想它作爲帳戶的子。

好吧,那麼你需要排除Contact,並包括Account

XSLT 1.0

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

<xsl:strip-space elements="*"/> 
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 

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

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

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

</xsl:stylesheet> 
+0

謝謝!像魅力一樣工作。 – Apojoost 2014-09-30 21:39:56