Xml XSLT-在text()节点中的给定位置之前获取子字符串
我在文档中有这样一个xml节点Xml XSLT-在text()节点中的给定位置之前获取子字符串,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我在文档中有这样一个xml节点 <full>Solid biofuels - Determination of moisture content - Oven dry method - Part 2: Total moisture - Simplified method</full> 我需要在的第三个-之前获取内容,其余内容应在范围内 我正在使用XSLT来解决这个问题。我尝试过XSLT正则表达式、tokanize()函数、substring()函数,但找不到合适的方法
<full>Solid biofuels - Determination of moisture content - Oven dry method - Part 2: Total moisture - Simplified method</full>
我需要在
的第三个-
之前获取内容,其余内容应在
范围内
我正在使用XSLT来解决这个问题。我尝试过XSLT正则表达式、tokanize()函数、substring()函数,但找不到合适的方法
<xsl:template match="full">
<full>
<xsl:for-each select="tokenize(.,'-')">
<p1>
<xsl:if test="position()=1 or position()=2">
<xsl:value-of select="."/>
<xsl:text>-</xsl:text>
</xsl:if>
</p1>
<p2>
<xsl:if test="position() gt 2">
<xsl:value-of select="."/>
<xsl:text>-</xsl:text>
</xsl:if>
</p2>
</xsl:for-each>
</full>
</xsl:template>
-
-
有人能给我推荐一种方法吗。下面是使用
tokenize
<xsl:template match="full">
<xsl:copy>
<xsl:variable name="tokens" select="tokenize(text(), ' - ')" />
<xsl:variable name="tokenCount" select="count($tokens)" />
<p1>
<xsl:value-of select="$tokens[position() le 3]" separator=" - " />
<xsl:if test="$tokenCount > 3"> - </xsl:if>
</p1>
<xsl:if test="$tokenCount > 3">
<p2>
<xsl:value-of select="$tokens[position() gt 3]" separator=" - " />
</p2>
</xsl:if>
</xsl:copy>
</xsl:template>
-
您是否可以显示当前尝试的代码,因为您的尝试可能并不遥远,而且更容易纠正您的尝试,而不是从头开始编写代码。谢谢@TimC,添加了我尝试过的示例代码。效果非常好。谢谢
<xsl:template match="full">
<xsl:copy>
<xsl:variable name="tokens" select="tokenize(text(), ' - ')" />
<xsl:variable name="tokenCount" select="count($tokens)" />
<p1>
<xsl:value-of select="$tokens[position() le 3]" separator=" - " />
<xsl:if test="$tokenCount > 3"> - </xsl:if>
</p1>
<xsl:if test="$tokenCount > 3">
<p2>
<xsl:value-of select="$tokens[position() gt 3]" separator=" - " />
</p2>
</xsl:if>
</xsl:copy>
</xsl:template>