【问题标题】:Redact XML using XSLT使用 XSLT 编辑 XML
【发布时间】:2015-08-12 14:41:18
【问题描述】:

想知道是否有人可以帮助解决我面临的 XSLT 问题。

我正在尝试创建一个 xslt 脚本,该脚本将输入一个 xml 文档并将几个字段的值更改为“xxxx”输入 xml 中的字段具有特定值(例如,如果用户名是 jbond)

如果可能的话,我希望在我的 XSLT 中包含这种情况,但是我遇到了困难。

我目前的 XML、XSLT、Output 和预期的输出如下

XML:

     <?xml version="1.0"?>
        <?xml-stylesheet type="text/xsl"?>
        <rootDoc>
        <user>test</user>
        <tel>12345</tel>
        <zip>abcd</zip>
</rootDoc>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>

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

<xsl:template match="tel/text()">XXXX</xsl:template>
<xsl:template match="zip/text()">XXXX</xsl:template>

</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<rootDoc/>

预期:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?><rootDoc>
   <user>test</user>
   <tel>XXXX</tel>
   <zip>XXXX</zip>
</rootDoc>

【问题讨论】:

    标签: java xml xslt xml-parsing transform


    【解决方案1】:

    如果您不理会身份转换,而是添加特定匹配项,XSLT 将自动找到最接近的匹配项。您可以自定义 select="" 或根据需要添加更多模板。 hth

        <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
        <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    <xsl:template match="tel"><tel>XXXX</tel></xsl:template>
    <xsl:template match="zip"><zip>XXXX</zip></xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    【解决方案2】:

    将测试放入恒等转换模板没有多大意义,如果您想在条件成立时执行某些更改,那么模板

    <xsl:template match="tel/text()">XXXX</xsl:template>
    <xsl:template match="zip/text()">XXXX</xsl:template>
    

    应该改为

    <xsl:template match="rootDoc[user = 'test']/tel/text()">XXXX</xsl:template>
    <xsl:template match="rootDoc[user = 'test']/zip/text()">XXXX</xsl:template>
    

    可以加入的

    <xsl:template match="rootDoc[user = 'test']/tel/text() | rootDoc[user = 'test']/zip/text()">XXXX</xsl:template>
    

    条件单一,假设是rootNode,可以使用

    <xsl:template match="/rootNode[not(user = 'test')]">
      <xsl:copy-of select="."/>
    </xsl:template>
    

    然后其他情况由身份转换和专用模板处理。

    【讨论】:

    • 感谢您的回复,您知道是否有可能有一个更通用的解决方案,它只运行一次条件而不是每个节点。在最终文档中,我们希望清理几个字段,因此我们正在努力减少重复条件的数量
    • 如果您知道在条件不成立的情况下不想更改任何内容,那么您可以为根元素使用模板,该模板只是复制所有内容而不是应用模板。
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2018-05-11
    相关资源
    最近更新 更多