Xml 如何在XSLT2 replace()函数中进行“混合匹配”实体转换?

Xml 如何在XSLT2 replace()函数中进行“混合匹配”实体转换?,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我正在试验XSLT2,使用基于答案的样式表: 用于进行多次更换,例如: <?xml version="1.0" encoding="utf-8"?> <xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1"> <file> <source>abc &lt;field1&gt; def &lt;field2&gt; gh

我正在试验XSLT2,使用基于答案的样式表:

用于进行多次更换,例如:

<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
  <file>
    <source>abc &lt;field1&gt; def &lt;field2&gt; ghi</source>
  </file>
</xliff>
致:

但是,我的转换无效,因此出现以下错误:

Error on line 12 column 54 of my.xsl:
  SXXP0003: Error reported by XML parser: The value of attribute "select" associated with an
  element type "null" must not contain the '<' character.
如果我使用select=replace.,“.*”,“phF/phgt;”然后我得到了。。。在输出中

如果我使用,我会引入其他问题,因为在我想保持不变的领域中可能会有其他实体。如果我使用我丢失了大部分xml—是否有其他类似的“混合和匹配”方式?

如果字符串出现在源文档中,则文档的XDM树表示形式将包含字符“问题在于:

在提供的XML文档上应用此转换时:

产生了想要的结果:


说明:正确使用XSLT 2.0说明,感谢您的回答Michael-我已经更新了问题,以准确显示我所做的尝试。正则表达式的“搜索”位工作得很好,正如您所说的那样-但是我在“替换”方面遇到了问题-问题是@Jack Douglas想要简单地通过字符串替换生成元素-这是不可能的。元素是节点,不能作为字符串的子字符串创建。解决方法是使用-就像我的回答中那样。这是可能的,但不是作为字符串替换。难道你不想在xsl:matching子字符串中使用吗?我已经根据我的实际需要对其进行了调整-这很容易,因为你的回答正确且非常清楚,尽管我认为@DevNull是对的-只是前一个问题的一些细微的继承:-@DevNull:为什么?OP本人在他的问题中使用了F。@Jack Douglas和DevNull:我相应地修改了答案。请注意,这不仅仅是,而是。适合这个问题,但regex-group1非常方便了解,谢谢-
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
  <file>
    <source>abc <ph>&lt;field1&gt;</ph> def <ph>&lt;field2&gt;</ph> ghi</source>
  </file>
</xliff>
Error on line 12 column 54 of my.xsl:
  SXXP0003: Error reported by XML parser: The value of attribute "select" associated with an
  element type "null" must not contain the '<' character.
<xsl:sequence select="replace(., '&lt;(.*?)&gt;', '<ph>F</ph>')"/>
 <xsl:template match="node()|@*">
   <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="source/text()">
  <xsl:analyze-string select="." regex="&lt;(.*?)&gt;">
    <xsl:matching-substring>
      <ph><xsl:value-of select="regex-group(1)"/></ph>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
     <xsl:sequence select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
 </xsl:template>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
    <file>
        <source>abc &lt;field1&gt; def &lt;field2&gt; ghi</source>
    </file>
</xliff>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
      <file>
            <source>abc <ph>field1</ph> def <ph>field2</ph> ghi</source>
      </file>
</xliff>