Xslt 如何检查仅包含字符+;使用正则表达式的空格和'p'
我想检查是否只包含字符+空格和Xslt 如何检查仅包含字符+;使用正则表达式的空格和'p',xslt,xpath,Xslt,Xpath,我想检查是否只包含字符+空格和节点 输入: <root> <used><p>String 1</p></used> <used>string 2<p>string 3</p></used> <used>string 4</used> <used><image>aaa.jpg</image>para</used
节点
输入:
<root>
<used><p>String 1</p></used>
<used>string 2<p>string 3</p></used>
<used>string 4</used>
<used><image>aaa.jpg</image>para</used>
字符串1
字符串2字符串3
字符串4
aaa.jpgpara
输出应为:
<ans>
<abc>string 1</abc>
<abc>string 4</abc>
</ans>
字符串1
字符串4
试用代码:
<ans>
<abc>
<xsl:template match="root">
<xsl:choose>
<xsl: when test="getCode/matches(text(),'^[a-zA-Z0-9]+$')">
<xsl:text>text()</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
</abc>
</ans>
正文()
我尝试过的代码没有按预期工作。我怎样才能解决这个问题?非常感谢。我使用的是XSLT 2.0,您可以使用以下XSLT-2.0样式表来获得所需的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Handle the <root> element -->
<xsl:template match="/root">
<ans>
<xsl:apply-templates select="used" />
</ans>
</xsl:template>
<!-- Create <abc> elements for every matching element -->
<xsl:template match="used[not(*) and matches(text(),'^[\sa-zA-Z0-9]+$')] | used[not(text()) and matches(p/text(),'^[\sa-zA-Z0-9]+$')]/p">
<abc><xsl:copy-of select="text()" /></abc>
</xsl:template>
<!-- Remove all spurious text nodes -->
<xsl:template match="text()" />
</xsl:stylesheet>
其结果是
<?xml version="1.0" encoding="UTF-8"?>
<ans>
<abc>String 1</abc>
<abc>string 4</abc>
</ans>
字符串1
字符串4
根据您的描述,有三个元素符合您的标准:1、2和4。它们都“只包含字符+空格和节点”。@zx485string 2string 3
此处。
包含
和文本。因此,它不应该被改变。仅文本或仅p
不适用于两者。附加信息有帮助。我改变了答案。