首页 > 解决方案 > 如果包含字符串、为空或不存在,XSLT 2.0 替换元素值

问题描述

我正在尝试对如下所示的 XML 文档执行转换:

<AutoInsurance>
    <Vehicles>
        <Vehicle>
            <Id>1</Id>
            <Make>HONDA</Make>
            <Model>ACCORD</Model>
            <SubModel>EX</SubModel>
        </Vehicle>
    </Vehicles>
</AutoInsurance>

如果 SubModel 元素包含有用的数据,我想维护它。但是,如果元素包含'-',''。或不存在,我想用“NA”替换该值。

例如:

<AutoInsurance>
    <Vehicles>
        <Vehicle>
            <Id>1</Id>
            <Make>HONDA</Make>
            <Model>ACCORD</Model>
            <SubModel>_</SubModel>
        </Vehicle>
    </Vehicles>
</AutoInsurance>
<AutoInsurance>
    <Vehicles>
        <Vehicle>
            <Id>1</Id>
            <Make>HONDA</Make>
            <Model>ACCORD</Model>
            <SubModel></SubModel>
        </Vehicle>
    </Vehicles>
</AutoInsurance>
<AutoInsurance>
    <Vehicles>
        <Vehicle>
            <Id>1</Id>
            <Make>HONDA</Make>
            <Model>ACCORD</Model>
        </Vehicle>
    </Vehicles>
</AutoInsurance>

都应该转变为

<AutoInsurance>
    <Vehicles>
        <Vehicle>
            <Id>1</Id>
            <Make>HONDA</Make>
            <Model>ACCORD</Model>
            <SubModel>NA</SubModel>
        </Vehicle>
    </Vehicles>
</AutoInsurance>

任何帮助是极大的赞赏!

标签: xmlxsltxslt-2.0

解决方案


您可以使用应导致“NA”的字符串声明一个变量。

并像这样使用它:

<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="#all"
  >
  
  <!-- identity transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:variable name="wrongStrings" as="xs:string*" select="'-','_','.'" />
  
  <xsl:template match="Vehicle">
    <xsl:apply-templates select="*[not(self::SubModel)]"/>
    <xsl:variable name="subModelContent" as="xs:string" select="if(SubModel/text()) then SubModel/text() else '-'"/>
    <SubModel>
      <xsl:value-of select="if($subModelContent=$wrongStrings) then 'NA' else SubModel/text()"/>
    </SubModel>
  </xsl:template>
  
</xsl:stylesheet>

当你有这个 xml 时:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <AutoInsurance>
    <Vehicles>
      <Vehicle>
        <Id>1</Id>
        <Make>HONDA</Make>
        <Model>ACCORD</Model>
        <SubModel>_</SubModel>
      </Vehicle>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Vehicle>
        <Id>1</Id>
        <Make>HONDA</Make>
        <Model>ACCORD</Model>
        <SubModel></SubModel>
      </Vehicle>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Vehicle>
        <Id>1</Id>
        <Make>HONDA</Make>
        <Model>ACCORD</Model>
      </Vehicle>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Vehicle>
        <Id>1</Id>
        <Make>HONDA</Make>
        <Model>ACCORD</Model>
        <SubModel>ABC</SubModel>
      </Vehicle>
    </Vehicles>
  </AutoInsurance>
</root>

结果将是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <AutoInsurance>
    <Vehicles>
      <Id>1</Id>
      <Make>HONDA</Make>
      <Model>ACCORD</Model>
      <SubModel>NA</SubModel>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Id>1</Id>
      <Make>HONDA</Make>
      <Model>ACCORD</Model>
      <SubModel>NA</SubModel>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Id>1</Id>
      <Make>HONDA</Make>
      <Model>ACCORD</Model>
      <SubModel>NA</SubModel>
    </Vehicles>
  </AutoInsurance>
  <AutoInsurance>
    <Vehicles>
      <Id>1</Id>
      <Make>HONDA</Make>
      <Model>ACCORD</Model>
      <SubModel>ABC</SubModel>
    </Vehicles>
  </AutoInsurance>
</root>

推荐阅读