Xml XSLT-replace";“价值”;元素。“条件重放”;“关键”;

Xml XSLT-replace";“价值”;元素。“条件重放”;“关键”;,xml,xslt,Xml,Xslt,我有一个带有PII的XML 示例XML: <DictionarySerializer> <dictionary xmlns="http://www.kmanage.com/xml/serialization"> <item> <key>FirstName</key> <value>John</value> <type>String</type>

我有一个带有PII的XML

示例XML:

<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:doc="http://www.kmanage.com/xml/serialization">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

 <xsl:template match="doc:item[doc:key/text() = 'FirstName']/doc:value/text()">
   <xsl:text>********</xsl:text>
 </xsl:template>
</xsl:stylesheet>
<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>

名字
约翰
一串
约翰
名字
约翰
一串
约翰
中间名
现状
一串
现状
姓氏
窦
一串
窦
我需要转换“值”元素。条件重放“键”

我的XSLT用于替换一个“键”的“值”:

<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:doc="http://www.kmanage.com/xml/serialization">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

 <xsl:template match="doc:item[doc:key/text() = 'FirstName']/doc:value/text()">
   <xsl:text>********</xsl:text>
 </xsl:template>
</xsl:stylesheet>
<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>

********
我需要替换“值”,若“键”等于名字、姓氏、出生日期、护照等等

我的结果:

<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>John</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:doc="http://www.kmanage.com/xml/serialization">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

 <xsl:template match="doc:item[doc:key/text() = 'FirstName']/doc:value/text()">
   <xsl:text>********</xsl:text>
 </xsl:template>
</xsl:stylesheet>
<DictionarySerializer>
  <dictionary xmlns="http://www.kmanage.com/xml/serialization">
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>FirstName</key>
      <value>********</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">John</value>
      </history>
    </item>
    <item>
      <key>MiddleName</key>
      <value>quo</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">quo</value>
      </history>
    </item>
    <item>
      <key>LastName</key>
      <value>Dou</value>
      <type>String</type>
      <history>
        <value stamp="201405301854095003707" owner="admin" type="String">Dou</value>
      </history>
    </item>
  </dictionary>
</DictionarySerializer>

名字
********
一串
约翰
名字
********
一串
约翰
中间名
现状
一串
现状
姓氏
窦
一串
窦
如果不使用数组,如何执行此操作

你好,伊利亚
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:doc="http://www.kmanage.com/xml/serialization">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
 <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
 </xsl:template>
 <xsl:template match="doc:item[doc:key/text() = 'FirstName']/doc:value/text()">
 <xsl:text>********</xsl:text>
 </xsl:template>
 <xsl:template match="doc:item[doc:key/text() = 'LastName']/doc:value/text()">
 <xsl:text>******** lastName replacement</xsl:text>
 </xsl:template>
 </xsl:stylesheet>
******** ********姓氏替换
此样式表过滤掉与列表匹配的
键的

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="http://www.kmanage.com/xml/serialization" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="doc:value/text()">
    <xsl:variable name="associated-key" select="../preceding-sibling::doc:key/text()"/>
    <xsl:choose>
      <xsl:when test="$associated-key = 'FirstName' or $associated-key = 'LastName' or $associated-key = 'DateOfBirth'">********</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

********

通过将筛选键列表存储在变量中,并使用XPath的
contains
函数查看当前键是否匹配,您可以更加优雅。

假设您希望平等对待所有键,您可以使用:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="http://www.kmanage.com/xml/serialization">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="doc:item[doc:key = 'FirstName' or doc:key = 'LastName' or doc:key = 'DateOfBirth' or doc:key = 'Passport' or doc:key = 'and so on...']/doc:value/text()">
    <xsl:text>********</xsl:text>
  </xsl:template>
</xsl:stylesheet>

********

虽然我个人更喜欢为这些东西创建一个查找表。您可以使用exslt进行以下操作:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:doc="http://www.kmanage.com/xml/serialization"
                xmlns:exsl="http://exslt.org/common">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="keysXml">
        <keys>
            <key>FirstName</key>
            <key>LastName</key>
            <key>DateOfBirth</key>
            <!-- add all required keys here -->
        </keys>
    </xsl:variable>
    <xsl:variable name="keys" select="exsl:node-set($keysXml)/keys" />

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

    <xsl:template match="doc:value">
        <xsl:choose>
            <xsl:when test="$keys/key = ../doc:key">
                <xsl:copy>
                    <xsl:text>********</xsl:text>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

名字
姓氏
出生日期
********

通过这种方式,您只需为另一个键添加另一个条目,这使得可维护性更加容易。

如果您想在样式表中维护PII键列表,那么您可以指定一个键名称的分隔列表,并测试
doc:key的值是否(分隔符作为前缀和后缀连接在一起,以避免对键名的部分匹配)包含在分隔的PII键列表中

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:doc="http://www.kmanage.com/xml/serialization">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="doc:item[contains(
                                   '|FirstName|LastName|DateOfBirth|Passport|',
                                    concat('|', doc:key, '|') 
                                   )]/doc:value/text()">
        <xsl:text>********</xsl:text>
    </xsl:template>
</xsl:stylesheet>

********

XSLT没有“数组”的概念。您可以做的最好的事情是编辑问题,向我们展示您试图生成的XML结果,然后我们可以建议实现这一点的最佳方法。我还不清楚您需要什么。是否要删除
,如果
与有限的一组密钥中的一个匹配?不要删除。我要替换/更改t如果匹配的是有限的一组密钥中的一个,那么他就会知道。密钥集也可能会更改。因此,我想使用“数组”。注意:对不起,我的英语很好。我同意,它是有效的。这是一个xml示例。工作xml包含更多密钥。密钥集也可能会更改。谢谢。我将此转换与c#一起使用。xmlns:exsl=“”>不工作。至于我,工作xmlns:exsl=“urn:schemas-microsoft-com:xslt”