xml - 非常简单的 XSL/XPath 健全性检查

标签 xml xslt xpath

我确信这将是一个非常简单的问题。我有一个 xml 文档,正在通过 XSL 进行转换。该 xml 的重要部分如下所示:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Transaction>
    <EnrollmentModel>
      <FutureContributionsModel>
        <FutureContributionsElectionType>ACertainThirdParty</FutureContributionsElectionType>
      </FutureContributionsModel>
    </EnrollmentModel>
  <Transaction>
</root>

如果 <FutureContributionsElectionType> 的值我想添加以下内容事实上,等于 ACertainThirdParty :

<fo:table-row>
    <fo:table-cell>
        <fo:block font-family="verdanaPS" font-size="9" padding-bottom="15px" padding-top="10px">
                        The Participant has successfully opted in to use ACertainThirdParty as the managed provider for the account.
        </fo:block>
    </fo:table-cell>
</fo:table-row>

请注意,只有一个第三方,因此我不需要获取自定义文本的节点值,我可以将其硬编码在那里。

如果 <FutureContributionsElectionType> 的值不等于ACertainThirdParty ,我不想添加一大堆其他东西。

这是我尝试过的:

所以这似乎是 <xsl:choose> 的工作和<xsl:when>/<xsl:otherwise> , 正确的? 这是我得到的:

<xsl:choose>
  <xsl:when test="FutureContributionsModel/FutureContributionsElectionType='ACertainThirdParty'">
    <fo:table-row>
      <fo:table-cell>
        <fo:block font-family="verdanaPS" font-size="9" padding-bottom="15px" padding-top="10px">
                        The Participant has successfully opted in to use ACertainThirdParty as the managed provider for the account.
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:when>
  <xsl:otherwise>
    <fo:table-row>
      <fo:table-cell>
          ...
          Lots of stuff
          ...
      </fo:table-cell>
    </fo:table-row>
  </xsl:otherwise>
</xsl:choose>

但是当我转换它时,其他代码会被命中,而不是正确的代码(在我的 xml 中,值确实是 ACertainThirdParty 。我猜我的问题是我不知道 XPath,所以我可能假设我能做我做不到的事情。这是怎么回事?

最佳答案

很可能上下文(当前节点)不是事务

您可以使用绝对 XPath 表达式:

/*/Transaction/EnrollmentModel/FutureContributionsModel/FutureContributionsElectionType='ACertainThirdParty'

更好的是,避免使用显式条件 - 使用模板和模板匹配模式:

<xsl:template match="FutureContributionsElectionType[.='ACertainThirdParty']">

  <!-- Specific Processing Here  -->
</xsl:template>

<xsl:template match="FutureContributionsElectionType[not(.='ACertainThirdParty')]">

  <!-- Other Specific Processing Here  -->
</xsl:template>

关于xml - 非常简单的 XSL/XPath 健全性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11887355/

相关文章:

xml - WiX Installer : using xslt with heat. exe 更新属性

xml - XPATH查询使用xml中的值

xml - 检查XML DOM XMLDOC时出错

java - 将 xml 从第 n 个元素拆分为第 x 个元素

java - XSLT 参数不起作用

xml - XML 中属性的名称->值对是否被视为由 XPATH 解释的两个节点?

c# - 向 MemoryStream XML 添加新属性

sql - SQL 中 XML 分解的替代方案

php - 是否应该在服务器端使用 XML,而在客户端使用 JSON?

html - 从 XML 结构中的纯文本创建 HTML 列表