【问题标题】:Test mutple tags if value is empty with XSLT如果值为空,则使用 XSLT 测试多个标签
【发布时间】:2020-04-08 08:29:58
【问题描述】:

我有以下 XML

<?xml version="1.0" encoding="utf-8"?>
<Subjects>
  <Subject>
    <Title>Mr</Title>
    <Firstname></Firstname>
    <Surname></Surname>
    <Email></Email>
    <Incidents>
      <Incident>
        <ReferenceNo>meuen</ReferenceNo>
      </Incident>
    </Incidents>
    <TelephoneNos />
    <Employers />
    <BankAccounts />
  </Subject>
  <Subject>
    <Title>Mr</Title>
    <Firstname>Test</Firstname>
    <Surname>Test</Surname>
    <Email>test@test.com</Email>
    <Incidents>
      <Incident>
        <ReferenceNo>jkcsdlkjsdl</ReferenceNo>
      </Incident>
    </Incidents>
    <TelephoneNos />
    <Employers />
    <BankAccounts />
  </Subject>
</Subjects>

我需要使用 XSLT 检查 ReferenceNo 字段是否为空

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://tempuri.org/">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="/">
        <Result xmlns="http://tempuri.org/">

            <xsl:for-each select="/Subjects/Subject">

                <xsl:variable name="output" select="Incidents/Incident/ReferenceNo"/>
                <xsl:choose>
                    <xsl:when test="string-length($output) > 0">
                        <Response>Y</Response>
                    </xsl:when>
                    <xsl:otherwise>
                        <Response>N</Response>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>

        </Result>
    </xsl:template>

</xsl:stylesheet>

但这仅检查第一个节点,而不是两个或所有 ReferenceNo 标记。

我怎样才能完成这个结果。

我已经在 xsl:foeach 中尝试过,这显然会在上述 XML 的实例中重复我的 tag1 和 tag2 节点两次

我希望response 的值是 Y,如果其中一个标签被填充,N 值,如果两个标签都没有被填充

这是我得到的当前响应

<Result xmlns="http://tempuri.org/" xmlns:x="http://tempuri.org/">
   <Response>Y</Response>
   <Response>Y</Response>
</Result>

我不想Response被重复

【问题讨论】:

  • 测试的真实结果应该是什么:(1) 所有ReferenceNo 为空,或(2) 至少一个ReferenceNo 为空?
  • 我对帖子做了一些修改,这有帮助吗?

标签: xml xslt xslt-1.0


【解决方案1】:

本次测试:

<xsl:when test="Subjects/Subject/Incidents/Incident/ReferenceNo[string-length() > 0]">

或者,如果您愿意:

<xsl:when test="Subjects/Subject/Incidents/Incident/ReferenceNo[text()]">

当至少有一个非空ReferenceNo 元素时返回true。

测试可以(并且应该)从/ 根节点的上下文中应用。不需要xsl:for-each 指令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多