简体   繁体   English

在替换文本中的xml节点时遇到问题

[英]Got problems with replacing xml nodes within text

I'm working on XML-to-XML transformation (Windows 10, Oxygen XML Editor) and got this task: replace <xref id="id1">text</xref> with id1 . 我工作的XML到XML的转换(视窗10,氧XML编辑器),并得到了这个任务:更换<xref id="id1">text</xref>id1

I've done some work but can't get why doesn't scenario replace all xref-s in parentheses. 我已经做了一些工作,但无法理解为什么方案不替换括号中的所有外部参照。 Any ideas? 有任何想法吗?

And just in case if somebody know how to remove parentheses outside of xref-s, please tell. 并且以防万一有人知道如何删除外部参照之外的括号,请告诉。 I was trying concat('(',$temp,')') but it also skips parenteses and concat('(',$temp,';') even doesn't work. 我正在尝试concat('(',$temp,')')但它也跳过了括号,而concat('(',$temp,';')甚至不起作用。

Here the example (I keep punctuation just in case): 这是示例 (我保留标点符号以防万一):

<section>
    <somenode>Lorem ipsum</somenode>
    <p>Lorem ipsum (<xref id="id1">TEXT1, 2014</xref>) dolor.</p>
    <p>Lorem ipsum (<xref id="id5">TEXT5., 2016</xref>) dolor.</p>
    <p>Lorem ipsum (<xref id="id6">TEXT6., 2004</xref>; <xref id="id7">TEXT7., 2014</xref>; <xref id="id8">TEXT8., 2012</xref>), dolor.</p>
    <p>Lorem ipsum (<xref id="id6">TEXT6., 2004</xref>; <xref id="id7">TEXT7., 2014</xref>; <xref id="id8">TEXT8., 2012</xref>), dolor.</p>
    ...
</section>
...

Here the result : 结果如下:

<section>
    <somenode>Lorem ipsum</somenode>
    <p>Lorem ipsum (id1) dolor.</p>
    <p>Lorem ipsum (id5) dolor.</p>
    <p>Lorem ipsum (id6; TEXT7., 2014; TEXT8., 2012), dolor.</p>
    <p>Lorem ipsum (TEXT6., 2004; id7; TEXT8., 2012), dolor.</p>
    ...
</section>
...

I expect : 我希望

<section>
    <somenode>Lorem ipsum</somenode>
    <p>Lorem ipsum (id1) dolor.</p>
    <p>Lorem ipsum (id5) dolor.</p>
    <p>Lorem ipsum (id6; id7; id8), dolor.</p>
    <p>Lorem ipsum (id6; id7; id8), dolor.</p>
    ...
</section>
...

and Here the scenario : 这里是场景

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template name="xrefs">
    <xsl:for-each select="section">
        <xsl:for-each select="p">
            <xsl:variable name="tempP">
                <xsl:value-of select="."/>
            </xsl:variable>
            <xsl:for-each select="xref">
                <xsl:variable name="temp">
                    <xsl:value-of select="."/>
                </xsl:variable>
                <xsl:value-of select="replace($tempP,$temp,./@id)"/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

<xsl:template match="/">
    <xsl:call-template name="xrefs"/>
</xsl:template>

got this task: replace <xref id="id1">text</xref> with id1 . 得到了以下任务:将<xref id="id1">text</xref>替换为id1

That could be done easily by: 可以通过以下方法轻松完成:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="xref">
    <xsl:value-of select="@id"/>
</xsl:template>

</xsl:stylesheet>

Applies to the following well-formed (!) input example: 适用于以下格式正确的(!)输入示例:

XML XML格式

<section>
    <somenode>Lorem ipsum</somenode>
    <p>Lorem ipsum (<xref id="id1">TEXT1, 2014</xref>) dolor.</p>
    <p>Lorem ipsum (<xref id="id5">TEXT5., 2016</xref>) dolor.</p>
    <p>Lorem ipsum (<xref id="id6">TEXT6., 2004</xref>; <xref id="id7">TEXT7., 2014</xref>; <xref id="id8">TEXT8., 2012</xref>), dolor.</p>
    <p>Lorem ipsum (<xref id="id6">TEXT6., 2004</xref>; <xref id="id7">TEXT7., 2014</xref>; <xref id="id8">TEXT8., 2012</xref>), dolor.</p>
    ...
</section>

the result will be: 结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<section>
   <somenode>Lorem ipsum</somenode>
   <p>Lorem ipsum (id1) dolor.</p>
   <p>Lorem ipsum (id5) dolor.</p>
   <p>Lorem ipsum (id6; id7; id8), dolor.</p>
   <p>Lorem ipsum (id6; id7; id8), dolor.</p>
    ...
</section>

And just in case if somebody know how to remove parentheses outside of xref-s, please tell. 并且以防万一有人知道如何删除外部参照之外的括号,请告诉。

That could be achieved by adding these two templates (requires XSLT 2.0): 可以通过添加以下两个模板来实现(需要XSLT 2.0):

<xsl:template match="text()[following-sibling::*[self::xref]][ends-with(., '(')]">
    <xsl:value-of select="substring(., 1, string-length() - 1) "/>
</xsl:template>

<xsl:template match="text()[preceding-sibling::*[self::xref]][starts-with(., ')')]">
    <xsl:value-of select="substring(., 2) "/>
</xsl:template>

Then the result will be: 那么结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<section>
   <somenode>Lorem ipsum</somenode>
   <p>Lorem ipsum id1 dolor.</p>
   <p>Lorem ipsum id5 dolor.</p>
   <p>Lorem ipsum id6; id7; id8, dolor.</p>
   <p>Lorem ipsum id6; id7; id8, dolor.</p>
    ...
</section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM