2015-02-24 47 views
1

我有一個帶有子節點的XML,我希望它們使用XSL在父級別節點中出現。我的XML不是很簡單,我對XSL的瞭解不是很好。 如果有人能爲我提供解決方案,我將會很棒。使用XML將子節點移動到父級別

我的樣本XML如下:

<env:Envelope 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    <env:Body> 
    <DPSretrieveResponse 
     xmlns="https://tpvs.hmrc.gov.uk/dps"> 
     <DPSdata 
     xmlns="http://www.govtalk.gov.uk/taxation/DPSwrapper/1"> 
     <DPSheader> 
      <Service>PAYE</Service> 
      <EntityType>EmpRef</EntityType>   
     </DPSheader>   
     <CodingNoticesP6P6B FormType="P6B" IssueDate="2008-05-06" SequenceNumber="1375" TaxYearEnd="2009" 
      xmlns="http://www.govtalk.gov.uk/taxation/CodingNoticesP6P6B/2"> 
      <EmployerRef>123/A6</EmployerRef> 
      <Name> 
      <Title>MR</Title> 
      <Forename>J V</Forename> 
      <Surname>Scanlon</Surname> 
      </Name> 
      <WorksNumber>SCA/466</WorksNumber> 
      <CodingUpdate> 
      <TaxCode>NT</TaxCode> 
      </CodingUpdate> 
     </CodingNoticesP6P6B> 
     </DPSdata> 
    </DPSretrieveResponse> 
    </env:Body> 
</env:Envelope> 

我要求是:

<env:Envelope 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <env:Body> 
    <DPSretrieveResponse 
     xmlns="https://tpvs.hmrc.gov.uk/dps"> 
     <DPSdata 
     xmlns="http://www.govtalk.gov.uk/taxation/DPSwrapper/1"> 
     <DPSheader> 
      <Service>PAYE</Service> 
      <EntityType>EmpRef</EntityType>   
     </DPSheader>   
     <CodingNoticesP6P6B FormType="P6B" IssueDate="2008-05-06" SequenceNumber="1375" TaxYearEnd="2009" 
      xmlns="http://www.govtalk.gov.uk/taxation/CodingNoticesP6P6B/2"> 
      <EmployerRef>123/A6</EmployerRef> 
      <Name>   
      </Name> 
      <Title>MR</Title> 
      <Forename>J V</Forename> 
      <Surname>Scanlon</Surname> 
      <WorksNumber>SCA/466</WorksNumber> 
      <CodingUpdate> 
      <TaxCode>NT</TaxCode> 
      </CodingUpdate> 
     </CodingNoticesP6P6B> 
     </DPSdata> 
    </DPSretrieveResponse> 
    </env:Body> 
</env:Envelope> 

簡單,標題,根據名稱標籤的名字及姓氏需要顯示在同一級別名稱;

<Name>   
</Name> 
<Title>MR</Title> 
<Forename>J V</Forename> 
<Surname>Scanlon</Surname> 

任何意見是高度讚賞。

謝謝。

+0

你嘗試過什麼到目前爲止,以及如何輸出你」現在變得與你所需要的不一樣了嗎?你能編輯這個問題來顯示你當前的XSLT嗎? – 2015-02-24 15:38:23

+0

如果您有一個標識模板(要複製所有節點),請創建另一個模板,選擇僅包含'的'Name'元素(可能將其添加爲適當的名稱空間的前綴)。它會將名稱的子元素移動到Name元素之前的位置。 – helderdarocha 2015-02-24 16:14:07

+0

另外,我們需要知道您使用的XSLT的版本。你有權訪問2.0或更高版本,還是僅限於1.0? – 2015-02-24 16:51:41

回答

0

與身份模板複製不應該在結果樹改變所有節點:

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

您可以創建一個單獨的模板根節點添加額外的命名空間聲明:

<xsl:template match="env:Envelope"> 
    <xsl:copy> 
     <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'" /> 
     <xsl:namespace name="soapenc" select="'http://schemas.xmlsoap.org/soap/encoding/'" /> 
     <xsl:namespace name="xsd" select="'http://www.w3.org/2001/XMLSchema'" /> 
     <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:template> 

最後,移動的Name孩子一個級別,首先你需要在那裏Name屬於申報命名空間:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
... xmlns:ns0="http://www.govtalk.gov.uk/taxation/CodingNoticesP6P6B/2"> 

所以你可以用你聲明的前綴限定匹配。然後,你只需撥打孩子<apply-templates/>而不復制當前元素:

<xsl:template match="ns0:Name"> 
    <xsl:apply-templates /> 
</xsl:template> 

這是一個完整的樣式表,這應該工作:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns0="http://www.govtalk.gov.uk/taxation/CodingNoticesP6P6B/2" 
    exclude-result-prefixes="ns0" 
    version="2.0"> 

    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="env:Envelope"> 
     <xsl:copy> 
      <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'" /> 
      <xsl:namespace name="soapenc" select="'http://schemas.xmlsoap.org/soap/encoding/'" /> 
      <xsl:namespace name="xsd" select="'http://www.w3.org/2001/XMLSchema'" /> 
      <xsl:apply-templates /> 
     </xsl:copy> 
    </xsl:template> 

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

    <xsl:template match="ns0:Name"> 
     <xsl:apply-templates /> 
    </xsl:template> 

</xsl:stylesheet> 
+0

太棒了!它完美的作品。非常感謝你的幫助。 – 2015-02-24 17:08:04