使用 xslt 屏蔽 xml 元素

Masking xml element using xslt

我是 xslt 实现的新手,想使用 xslt 进行 xml 到 xml 转换。我有以下具有多个层次结构的 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 吗?我将进一步改进它以满足我的要求。

I would like to mask the value of "PersonLegalID" element but rest of the whole xml has to be preserved(I want just 123456789 to be converted to *****6789).

在这种情况下,除了一些细节之外,您希望按原样复制所有内容,最好从 identity transform 模板开始,然后添加例外覆盖它。

假设相关 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 节点。