2015-07-10 72 views
0

我是xslt實現的新手,並想使用xslt進行xml-to-xml轉換。我有以下的XML結構,多個層次,使用xslt掩蓋xml元素

<GetData xmlns="http://www.hr-xml.org/3" releaseID="3.3"> 
<Application> 
    <Sender> 
     <ID>Person</ID> 
    </Sender> 
    <Receiver> 
     <Component>DataService</Component> 
    </Receiver> 
</Application> 
<CreationDateTime>2015-07-10</CreationDateTime> 
<DataArea> 
    <HRData> 
     <PersonDossier> 
      <MasterPerson> 
       <PersonID schemeID="MasterPersonId" schemeAgencyID="Agency">654321</PersonID> 
       <PersonLegalID schemeID="LegalID" schemeAgencyID="AgencyID">123456789</PersonLegalID> 
       <PersonName> 
        <FormattedName formatCode="GivenName, FamilyName">kjddfaad lsfjjo</FormattedName> 
        <GivenName>kjddfaad<GivenName> 
        <FamilyName>lsfjjo</FamilyName> 
       </PersonName> 
      </MasterPerson> 
     </MasterPersonDossier> 
    </HRData> 
</DataArea> 
</GetData> 

問題: 我想掩蓋「PersonLegalID」元素,但整個XML的其餘部分的價值已經被保存下來(我只想123456789被轉換爲***** 6789)。

有人可以爲此提出一個xslt嗎?我會進一步改進以符合我的要求。

+0

PersonLegalID中的值是否具有已知的固定長度? - P.S.請提供**格式正確的** XML輸入;你的許多元素都沒有正確關閉。 –

+0

是的,它是一個9個字符的長度值。 – Andy

回答

1

我想掩蓋「PersonLegalID」元素,但其餘的 整個XML已經被保存的值(我只想123456789是 轉換爲***** 6789)。

在這樣的,你想要的是除了一些細節的所有內容複製的情況下,這是最好的開始與恆等變換模板的規則,然後添加例外覆蓋它。

假設有問題的ID始終是9位長,你可以這樣做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns3="http://www.hr-xml.org/3"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="ns3:PersonLegalID/text()"> 
    <xsl:value-of select="concat('*****', substring(., 6))"/> 
</xsl:template> 

</xsl:stylesheet> 

注意使用一個命名空間前綴,以解決PersonLegalID節點。

+0

非常感謝!我會盡力建立你的意見。 – Andy