使用 xslt 屏蔽 xml 元素

编程入门 行业动态 更新时间:2024-10-28 04:30:03
本文介绍了使用 xslt 屏蔽 xml 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 xslt 实现的新手,想使用 xslt 进行 xml-to-xml 转换.我有以下具有多个层次结构的 xml 结构,

I am new to xslt implementation and would like to do a xml-to-xml transformation using xslt. I have the following xml structure with multiple levels of hierarchy,

<GetData xmlns="http://www.hr-xml/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).

Question: 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).

有人可以为此建议一个xslt吗?我会进一步改进它以满足我的要求.

Can someone please suggest an xslt for this? I will further improve it to meet my requirements.

推荐答案

我想屏蔽PersonLegalID"元素的值,但其余部分必须保留整个 xml(我只想要 123456789转换为 *****6789).

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).

在这种情况下,如果您想复制除少数细节之外的所有内容,最好以身份转换模板作为规则开始,然后添加例外以覆盖它.

In cases like this, where you want to copy everything as is except for a few details, it's best to start with the identity transform template as the rule, then add exceptions to override it.

假设有问题的 ID 总是 9 位数字,你可以这样做:

Assuming the ID in question is always 9 digits long, you could do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3/1999/XSL/Transform"
xmlns:ns3="http://www.hr-xml/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 节点.

Note the use of a namespace prefix to address the PersonLegalID node.

这篇关于使用 xslt 屏蔽 xml 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 01:10:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏蔽   元素   xslt   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!