XSLT - 替换"value"元素。条件恢复"key"



我有一个带有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>

我需要转换"value"元素。条件替换"key"

我的XSLT用于替换一个"key"的"value":

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

我需要替换"value"如果"key"等于FirstName, LastName, DateOfBirth, Passport等…

我的结果:

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

如何在不使用数组的情况下做到这一点?

问候,Ilya

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

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

这样,您所需要做的就是为另一个键添加另一个条目,这使得可维护性容易得多。

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

这个样式表过滤掉匹配列表的key中的value:

<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函数查看当前键是否匹配,从而实现更优雅的方式。

如果您希望在样式表中维护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>

最新更新