如何在 XML 中关闭节点元素之前查找数字
How to find Numbers before closing node Element in XML
我尝试在关闭元素之前查找字符串,例如(277, 288
) 然后替换 </code> return s/b <code>277
。 (<)
不支持我的正则表达式
输入XML
<root>
<p>Magno v College Network, Inc. (2016) 1 CA5th 277, 288</p>
<p>Magno v College Network, Inc. (5, 2017) 1 CA5th 15, 288 SA</p>
</root>
XSLT
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()">
<xsl:value-of select="replace(., '([0-9]+),\s[0-9]+(<)', '')"/>
</xsl:template>
预期输出
<root>
<p>Magno v College Network, Inc. (2016) 1 CA5th 277</p>
<p>Magno v College Network, Inc. (5, 2017) 1 CA5th 15, 288 SA</p>
</root>
什么样的数字,只有正整数,没有任何前导 +
或 -
符号?无论如何,如果您使用正则表达式进行匹配,那么元字符 $
表示字符串末尾的匹配,也许在您的表达式中使用它,如 '([0-9]+),\s[0-9]+$'
,就足够了。
我尝试在关闭元素之前查找字符串,例如(277, 288
) 然后替换 </code> return s/b <code>277
。 (<)
不支持我的正则表达式
输入XML
<root>
<p>Magno v College Network, Inc. (2016) 1 CA5th 277, 288</p>
<p>Magno v College Network, Inc. (5, 2017) 1 CA5th 15, 288 SA</p>
</root>
XSLT
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()">
<xsl:value-of select="replace(., '([0-9]+),\s[0-9]+(<)', '')"/>
</xsl:template>
预期输出
<root>
<p>Magno v College Network, Inc. (2016) 1 CA5th 277</p>
<p>Magno v College Network, Inc. (5, 2017) 1 CA5th 15, 288 SA</p>
</root>
什么样的数字,只有正整数,没有任何前导 +
或 -
符号?无论如何,如果您使用正则表达式进行匹配,那么元字符 $
表示字符串末尾的匹配,也许在您的表达式中使用它,如 '([0-9]+),\s[0-9]+$'
,就足够了。