xml - XML/XSL if else 子字符串
问题描述
如果条目太长,我想用 if 子句对XML/XSL中的变量进行子串化。
我尝试过这样的事情,但它不是那样工作的。
<xsl:variable id="newId" select="./newId"/>
<xsl:template match="newId">
<xsl:choose>
<xsl:when test="string-length() < 15">
<xsl:value-of select="newId"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(.,1,15)" />
<br>
<xsl:value-of select="substring(.,16)" />
</br>
</xsl:otherwise>
</xsl:choose>
解决方案
您的代码中有几处需要更改。
- xsl:variable 有一个 'name' 属性,而不是 'id'
- 字符串长度函数需要一个参数
这是我的做法:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="newId" select="'SomeText123456789'"/>
<xsl:choose>
<xsl:when test="string-length($newId) < 15">
<xsl:value-of select="$newId"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($newId,1,15)" />
<br>
<xsl:value-of select="substring($newId,16)" />
</br>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
看到它在这里工作:https ://xsltfiddle.liberty-development.net/jxDjind
推荐阅读
- javascript - PHP echo 未显示 SweetAlert2 弹出窗口
- oracle - Oracle XML ORA-00978:没有 GROUP BY 的嵌套组函数
- skype - 如何在没有 Azure AD 的情况下使用 Skype web sdk 登录
- javascript - 使用 CSS 让我的标签在移动解决方案上更好
- sorting - Elasticsearch:如果点击次数过多,则有条件/禁用排序
- mysql - SQL查询以获取按列分组的列的最新非空值
- version-control - DVC 如何将目录级别的差异存储到 DVC 缓存中?
- postgresql - 使用 Postgres 逻辑复制槽时如何限制 WAL 大小?
- apache-kafka - 永久 Kafka Streams/KSQL 保留策略
- git - Git - .config 中的命名约定?