gpt4 book ai didi

xml - 在输出地址节点之前检查 5 行中至少有 2 行存在于 XSL 1.0 中的地址上

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:53 25 4
gpt4 key购买 nike

我正在尝试创建一个地址来验证固定模式,它要求至少输入 5 行中的 2 行。

如果五行中至少有两行可用,则只显示地址节点。

有没有一种方法可以使用 XSL 进行检查

输入看起来像这样:

<Services Street="1 The Road " ExtraAddress="The Street" 
VillageTownName="" PostalTownName="" County="">

要求有效输出

<Address>
<line>1 The Road</line>
<line>The Street</line
</Address>

谢谢,

标记

最佳答案

计算所有具有值的属性,并使用该计数来驱动验证:

  <xsl:template match="Services">
<xsl:variable name="line-count"
select="count(
./@*[(name() = 'Street'
or name() = 'ExtraAddress'
or name() = 'VillageTownName'
or name() = 'PostalTownName'
or name() = 'County')
and string-length(.) &gt; 0]
)"
/>
<xsl:choose>
<xsl:when test="$line-count &gt; 1">
<xsl:text>Valid</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Invalid</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

如果这些是唯一允许出现在 Services 元素中的属性,那么您可以删除显式名称检查并使用 @* 作为选择器。

我已经使用 XSLT 1.0 对此进行了测试,它运行良好。

在有效情况下,您需要放置一个 apply-template 元素代替我的 text 元素,以打印 address元素。这是简单的部分。

关于xml - 在输出地址节点之前检查 5 行中至少有 2 行存在于 XSL 1.0 中的地址上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1444168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com