2015-02-24 29 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

相关问题