1

这个问题是这个问题的延伸。我有很多价值观,如下所示:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>foo bar</elem2>
    <elem3>foo bar foo</elem3>
    <elem4>foofoo</elem4>
</myStuff>

我一直在做一个copy-ofover MOST the elems (the selectin thecopy-of非常具体),然后对生成的 XML 进行查找和替换,但我想将两者结合起来。在我应用这个的大多数情况下,我会用下面的模板替换所说的任何<xsl:value-of select="whatever">内容<xsl:apply-templates select="whatever">

<xsl:template match="*">
    <xsl:variable name="rep1">
        <xsl:choose>
            <xsl:when test='matches(., ".*foo.*")'>
                <xsl:value-of select='replace(., "foo", "qwerty")' />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select='./text()' />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="rep2">
        <xsl:choose>
            <xsl:when test='matches(., ".*bar.*")'>
                <xsl:value-of select='replace($rep1, "bar", "myBar")' />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select='$rep1' />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$rep2" />
</xsl:template>

我想使用类似的模板来替换copy-of我的代码中的 (因为我正在做与其他文件相同的替换,所以我可以使用相同的模板),但我不确定如何做到这一点。

输出将如下所示:

<myOtherStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myOtherStuff>

感谢所有帮助,并提前感谢!

4

2 回答 2

2

使用另一种方法,此样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:variable name="vReps" as="element()*">
        <rep target="foo" newval="qwerty"/>
        <rep target="bar" newval="myBar"/>
    </xsl:variable>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:analyze-string select="." 
         regex="{string-join($vReps/concat('(',@target,')'),'|')}">
            <xsl:matching-substring>
                <xsl:value-of select="$vReps[@target=current()]/@newval"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>
</xsl:stylesheet>

输出:

<myStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myStuff>

此外,此输入(来自评论):

<myStuff>
    <elem>asdfooasdf</elem>
</myStuff>

输出:

<myStuff>
    <elem>asdqwertyasdf</elem>
</myStuff>

注意:这个 RegExp 联合一次执行所有替换。这可能与顺序fn:replace调用不同:假设此替换针对此字符串“AB”上的“A”->“B”和“B”->“C”,联合替换应输出“BC”并顺序调用输出“CC”。

注意 2:关于匹配顺序,请注意 RegExp 联合遵循自己的规则(请参阅规范,更具体If two alternatives within the supplied $pattern both match at the same position in the $input string, then the match that is chosen is the first.),并且顺序fn:replace调用严格遵循用户定义的顺序。

于 2010-09-01T18:17:04.313 回答
1

这种转变

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" >
 <xsl:output omit-xml-declaration="yes"/>

  <xsl:variable name="vReps" as="element()*">
   <rep target="foo" newval="qwerty"/>
   <rep target="bar" newval="myBar"/>
  </xsl:variable>

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="text()">
      <xsl:sequence select="my:myMultiRep(., $vReps)"/>
    </xsl:template>

    <xsl:function name="my:myMultiRep" as="xs:string">
      <xsl:param name="pText" as="xs:string"/>
      <xsl:param name="pReps" as="element()*"/>

      <xsl:sequence select=
       "if(not($pReps))
         then $pText
         else my:myMultiRep(replace($pText, $pReps[1]/@target, $pReps[1]/@newval),
                            subsequence($pReps, 2)
                           )
       "/>
    </xsl:function>
</xsl:stylesheet>

应用于提供的 XML 文档时

<myStuff>
    <elem1>asdf</elem1>
    <elem2>foo bar</elem2>
    <elem3>foo bar foo</elem3>
    <elem4>foofoo</elem4>
</myStuff>

产生想要的正确结果

<myStuff>
    <elem1>asdf</elem1>
    <elem2>qwerty myBar</elem2>
    <elem3>qwerty myBar qwerty</elem3>
    <elem4>qwertyqwerty</elem4>
</myStuff>
于 2010-09-01T17:42:20.963 回答