如何通过XSLT混淆XML中嵌套的性别相关字段信息?
解决XML中性别相关字段的XSLT混淆问题
需求说明
已完成XML大部分内容的混淆处理,剩余需处理以下性别相关数据:
- 将
wd:gender节点的wd:Descriptor属性值Male替换为Obfuscated Gender - 将
wd:gender子节点中wd:type属性为Gender_Code的wd:ID节点值M替换为Obfuscated Gender Code
要求修改后不破坏XML语法,且避免之前出现的修改被覆盖问题。
原XML内容
<?xml version='1.0' encoding='UTF-8'?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <wd:Report_Data xmlns:wd="urn:URN"> <wd:Report_Entry> <wd:Candidate_ID>CANDID</wd:Candidate_ID> <wd:First_Name>FIRSTNAME</wd:First_Name> <wd:Last_Name>LASTNAME</wd:Last_Name> <wd:Email>EMAIL</wd:Email> <wd:Job_Requisition wd:Descriptor="JR0202517 Graduate Intern Technical (Open)"> <wd:ID wd:type="WID">7f0219076bc1010a481ed02402b40000</wd:ID> <wd:ID wd:type="Job_Requisition_ID">JR0202517</wd:ID> </wd:Job_Requisition> <wd:Candidate_Stage1>REVIEW</wd:Candidate_Stage1> <wd:Candidate_Stage wd:Descriptor="Review"> <wd:ID wd:type="WID">31350d72f899430fb446b410c1e368cd</wd:ID> <wd:ID wd:type="Recruiting_Stage_ID">REVIEW</wd:ID> </wd:Candidate_Stage> <wd:Created_Moment>2023-02-24T03:54:51.948-08:00</wd:Created_Moment> <wd:Job_Application_Date>2023-03-17</wd:Job_Application_Date> <wd:Job_Application_Source wd:Descriptor="Social Network -> Facebook"> <wd:ID wd:type="WID">dc8bf79476611087891d570080ae09fc</wd:ID> <wd:ID wd:type="Applicant_Source_ID">Facebook</wd:ID> </wd:Job_Application_Source> <wd:Phone_Number>PHONE</wd:Phone_Number> <wd:Country>COUNTRY</wd:Country> <wd:Internal>0</wd:Internal> <wd:gender wd:Descriptor="Male"> <wd:ID wd:type="WID">dc8bf79476611087d99a5866c88b70e2</wd:ID> <wd:ID wd:type="Gender_Code">M</wd:ID> </wd:gender> <wd:fileName>FILENAME</wd:fileName> <wd:attachmentContent>BASE64</wd:attachmentContent> </wd:Report_Entry> </wd:Report_Data> </env:Body> </env:Envelope>
当前使用的XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="*:Email"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Email]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:First_Name"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Name]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:Last_Name"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Last Name]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:Phone_Number"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Phone]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:attachmentContent"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Base64 Attachment]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:fileName"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated FileName]</xsl:text> </xsl:copy> </xsl:template> </xsl:stylesheet>
预期输出结果
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <wd:Report_Data xmlns:wd="urn:URN"> <wd:Report_Entry> <wd:Candidate_ID>CANDID</wd:Candidate_ID> <wd:First_Name>[Obfuscated Name]</wd:First_Name> <wd:Last_Name>[Obfuscated Last Name]</wd:Last_Name> <wd:Email>[Obfuscated Email]</wd:Email> <wd:Job_Requisition wd:Descriptor="JR0202517 Graduate Intern Technical (Open)"> <wd:ID wd:type="WID">7f0219076bc1010a481ed02402b40000</wd:ID> <wd:ID wd:type="Job_Requisition_ID">JR0202517</wd:ID> </wd:Job_Requisition> <wd:Candidate_Stage1>REVIEW</wd:Candidate_Stage1> <wd:Candidate_Stage wd:Descriptor="Review"> <wd:ID wd:type="WID">31350d72f899430fb446b410c1e368cd</wd:ID> <wd:ID wd:type="Recruiting_Stage_ID">REVIEW</wd:ID> </wd:Candidate_Stage> <wd:Created_Moment>2023-02-24T03:54:51.948-08:00</wd:Created_Moment> <wd:Job_Application_Date>2023-03-17</wd:Job_Application_Date> <wd:Job_Application_Source wd:Descriptor="Social Network -> Facebook"> <wd:ID wd:type="WID">dc8bf79476611087891d570080ae09fc</wd:ID> <wd:ID wd:type="Applicant_Source_ID">Facebook</wd:ID> </wd:Job_Application_Source> <wd:Phone_Number>[Obfuscated Phone]</wd:Phone_Number> <wd:Country>COUNTRY</wd:Country> <wd:Internal>0</wd:Internal> <wd:gender wd:Descriptor="Obfuscated Gender"> <wd:ID wd:type="WID">dc8bf79476611087d99a5866c88b70e2</wd:ID> <wd:ID wd:type="Gender_Code">Obfuscated Gender Code</wd:ID> </wd:gender> <wd:fileName>[Obfuscated FileName]</wd:fileName> <wd:attachmentContent>[Obfuscated Base64 Attachment]</wd:attachmentContent> </wd:Report_Entry> </wd:Report_Data> </env:Body> </env:Envelope>
修改后的XSLT解决方案
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wd="urn:URN"> <!-- 基础复制模板,保留所有未指定修改的内容 --> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <!-- 已有的混淆模板 --> <xsl:template match="*:Email"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Email]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:First_Name"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Name]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:Last_Name"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Last Name]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:Phone_Number"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Phone]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:attachmentContent"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated Base64 Attachment]</xsl:text> </xsl:copy> </xsl:template> <xsl:template match="*:fileName"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>[Obfuscated FileName]</xsl:text> </xsl:copy> </xsl:template> <!-- 新增性别相关混淆模板 --> <!-- 替换wd:gender节点的wd:Descriptor属性值 --> <xsl:template match="wd:gender/@wd:Descriptor"> <xsl:attribute name="wd:Descriptor"> <xsl:text>Obfuscated Gender</xsl:text> </xsl:attribute> </xsl:template> <!-- 替换wd:gender下type为Gender_Code的wd:ID节点内容 --> <xsl:template match="wd:gender/wd:ID[@wd:type='Gender_Code']"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:text>Obfuscated Gender Code</xsl:text> </xsl:copy> </xsl:template> </xsl:stylesheet>
说明
- 新增
xmlns:wd="urn:URN"命名空间声明,确保模板能精准匹配wd前缀的节点和属性,避免通配匹配的歧义问题。 - 针对目标属性和节点编写更具体的匹配模板:
- 匹配
wd:gender节点的wd:Descriptor属性,直接替换属性值 - 匹配
wd:gender下的wd:ID节点且其wd:type属性为Gender_Code,替换节点文本内容
- 匹配
- XSLT模板遵循优先级规则,更具体的匹配模板会覆盖基础复制模板的处理,因此修改不会被覆盖,同时保留其他未修改内容的结构和值。
内容的提问来源于stack exchange,提问作者wolfsilver00
相关产品推荐
相关产品推荐

