用XSLT替换部分属性值

时间:2019-08-21 12:13:06

标签: xslt

我想用另一个值替换属性值的子字符串。在下面的示例中,我想采用属性tagName =“ blubb”的所有元素,并通过在其中找到字符串“ abc”并将其替换为xyz来替换其tagValue。另外,同一属性(如果存在)中的字符串“ def”应替换为AAA。

样本输入:

<shop>
<items>
    <item id="1">
        <tag tagName = "Description" tagValue ="Item 1" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "USD" />
        <tag tagName = "blubb" tagValue = "abc,def,ghi,jkl" />
    </item>
    <item id="2">
        <tag tagName = "Description" tagValue ="Item 2" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "EUR" />
        <tag tagName = "blubb" tagValue = "def,ghi,jkl" />
    </item>
    <item id="2">
        <tag tagName = "Description" tagValue ="Item 2" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "EUR" />
        <tag tagName = "blubb" tagValue = "abc,def,jkl" />
    </item>
</items>
</shop>

预期输出(abc替换为xyz,def替换为AAA)

<shop>
<items>
    <item id="1">
        <tag tagName = "Description" tagValue ="Item 1" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "USD" />
        <tag tagName = "blubb" tagValue = "xyz,AAA,ghi,jkl" />
    </item>
    <item id="2">
        <tag tagName = "Description" tagValue ="Item 2" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "EUR" />
        <tag tagName = "blubb" tagValue = "AAA,ghi,jkl" />
    </item>
    <item id="2">
        <tag tagName = "Description" tagValue ="Item 2" />
        <tag tagName = "Price" tagValue = "5.00" />
        <tag tagName = "Currency" tagValue = "EUR" />
        <tag tagName = "blubb" tagValue = "xyz,AAA,jkl" />
    </item>
</items>
</shop>

使用xslt可以吗?

谢谢!

更新-我尝试使用替换功能调整xsl-我之前有一个副本,并按照以下示例将其调整为副本,但是我现在不再获取任何数据,因此我尝试使其与再次复制 当我这样做时,它不会替代任何东西。我想那是因为我里面有twiche xsl:template,是吗?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:template match="items">
    <xsl:copy-of select="item[@type='DEVICE']/tag[@tagName='Currency' and starts-with(@tagValue,'EUR')]/.."/>
</xsl:template>
<xsl:template match="item/tag[@tagName='blubb']">
    <xsl:param name="tagValue" />
    <xsl:variable name="tagValue" select="replace($tagValue,'abc','xyz')"/>
    <xsl:variable name="tagValue" select="replace($tagValue,'def','AAA')"/>
</xsl:template>
</xsl:stylesheet>

3 个答案:

答案 0 :(得分:0)

您可以参考这篇文章:XSLT string replace

要恢复:

  • 对于XSLT 2.0:

    通过使用替换功能<xsl:variable name="text" select="replace($text,'word_to_be_replaced','word_to_replace')"/>

  • 对于XSLT 1.0:

    1. 测试要替换的字符串是否存在(<xsl:when test="contains($text, $word_to_be_replaced)">
    2. 如果是这样,请在要替换的单词({<xsl:value-of select="substring-before($text,$word_to_be_replaced)" />)之前输出子字符串,在(({<xsl:value-of select="substring-after($text,$word_to_be_replaced)" />))之后输出子字符串。

答案 1 :(得分:0)

当心使用简单的方法:

replace($value,'abc','XYZ')

当令牌仅包含子字符串abc时,您可能会得到误报-例如abcdefdeabcefdefabc

要确保只替换整个令牌abc,请使用:

replace($value, '(^|,)abc(,|$)', '$1XYZ$2')

演示https://xsltfiddle.liberty-development.net/ej9EGdo

答案 2 :(得分:0)

您可以尝试--

    <?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" omit-xml-declaration="no"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="tag[@tagName[.='blubb']]/@tagValue">
                <xsl:attribute name="tagValue">
                    <xsl:analyze-string select="." regex="(abc)|(def)">
                        <xsl:matching-substring>
                            <xsl:choose>
                                <xsl:when test="regex-group(1)">
                                    <xsl:text>xyz</xsl:text>
                                </xsl:when>
                                <xsl:when test="regex-group(2)">
                                    <xsl:text>AAA</xsl:text>
                                </xsl:when>
                            </xsl:choose>
                        </xsl:matching-substring>
                        <xsl:non-matching-substring>
                            <xsl:value-of select="."/>
                        </xsl:non-matching-substring>
                    </xsl:analyze-string>
                </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

使用此分析字符串