如何使用 xpath 编辑特定 xml

How to edit specific xml using xpath

我得到了以下 xml,它是使用 xslt 函数 json-to-xml() 生成的。我需要使用给定的 xpath 更新此 xml,如结果 xml 中所示。我需要 java 或 xslt 中的解决方案。任何帮助将非常感激。

XML

 <?xml version="1.0" encoding="UTF-8"?>
    <map xmlns="http://www.w3.org/2005/xpath-functions">
        <map key="Response">
            <map key="Headers">
                <string key="server">Lisa</string>
                <string key="Status-Code">200</string>
                <string key="Content-Type">applicaton/json</string>
            </map>
            <map key="Payload">
                <map key="root">
                    <array key="cars">
                        <map>
                            <string key="company">ccc</string>
                            <string key="model">mmm</string>
                        </map>
                        <map>
                            <string key="strength">666</string>
                            <string key="Capacity">333</string>
                        </map>
                    </array>
                    <array key="bikes">
                        <map>
                            <string key="company">abc</string>
                            <string key="model">2018</string>
                        </map>
                    </array>
                </map>
            </map>
        </map>
    </map>

XPATHS

/Response/Payload/root/cars[2]/strength=999
/Response/Payload/root/bikes/model=2019
/Response/Headers/server=WebSphere
/Response/Headers/Content-Type=text
/Response/Payload/root/cars[2]/Capacity=555
/Response/Payload/root/cars[1]/model=mmm1
/Response/Payload/root/bikes/company=xyz
/Response/Payload/root/cars[1]/company=ccc1
/Response/Headers/Status-Code=400

更新结果XML

<map xmlns="http://www.w3.org/2005/xpath-functions">
    <map key="Response">
        <map key="Headers">
            <string key="server">WebSphere</string>
            <string key="Status-Code">400</string>
            <string key="Content-Type">text</string>
        </map>
        <map key="Payload">
            <map key="root">
                <array key="cars">
                    <map>
                        <string key="company">ccc1</string>
                        <string key="model">mmm1</string>
                    </map>
                    <map>
                        <string key="strength">999</string>
                        <string key="Capacity">555</string>
                    </map>
                </array>
                <array key="bikes">
                    <map>
                        <string key="company">xyz</string>
                        <string key="model">2019</string>
                    </map>
                </array>
            </map>
        </map>
    </map>
</map>

我的尝试
我尝试使用 xslt 函数将此 xml 转换回 json,然后使用 com.jayway.jsonpath Jayway JsonPath 库将给定 xpath 上的 json 的 parse/change 值.最后使用 xslt 函数再次将 json 更改为 xml。这对我很有用! 但是在使用这个库之后,我开始在我的应用程序的其他部分面临问题:(。所有带有 json 请求正文的 post 请求开始给出 400 错误。错误是 "Error 400 The request sent by the client was syntactically incorrect" 。我不能没弄清楚问题,可能是由于不同的 jar 冲突。 所以我正在尝试一些其他的工作方式。还有其他像 Jayway JsonPath 这样的库吗?或者任何其他 solution/Suggestion 都会有很大帮助。

给定 XSLT 3 的一种方法可能是将您拥有的那些路径转换为 ​​XSLT 3 匹配模板,然后创建一个可以使用 transform 函数 (https://www.w3.org/TR/xpath-functions/#func-transform) 执行的样式表:

<?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"
    xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
    exclude-result-prefixes="xs"
    version="3.0">

    <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

    <xsl:output method="xml" indent="yes"/>

    <xsl:param name="paths" as="xs:string">/Response/Payload/root/cars[2]/strength=999
        /Response/Payload/root/bikes[1]/model=2019
        /Response/Headers/server=WebSphere
        /Response/Headers/Content-Type=text
        /Response/Payload/root/cars[2]/Capacity=555
        /Response/Payload/root/cars[1]/model=mmm1
        /Response/Payload/root/bikes[1]/company=xyz
        /Response/Payload/root/cars[1]/company=ccc1
        /Response/Headers/Status-Code=400</xsl:param>

    <xsl:param name="path-sequence" as="xs:string*" select="tokenize($paths, '\s+')!normalize-space()"/>

    <xsl:variable name="stylesheet">
        <axsl:stylesheet version="3.0">
            <axsl:mode on-no-match="shallow-copy"/>

            <xsl:for-each select="($path-sequence)">
                <xsl:variable name="steps" select="tokenize(., '/')"/>
                <axsl:template>
                    <xsl:attribute name="match">
                        <xsl:apply-templates select="tail($steps)" mode="step"/>
                    </xsl:attribute>
                    <axsl:copy>
                        <axsl:copy-of select="@*"/>
                        <xsl:value-of select="substring-after($steps[last()], '=')"/>
                    </axsl:copy>                   
                </axsl:template>
            </xsl:for-each>

        </axsl:stylesheet>
    </xsl:variable>

    <xsl:template match=".[not(contains(., '[')) and not(contains(., '='))]" mode="step">
        <xsl:if test="position() gt 1">/</xsl:if>
        <xsl:sequence select="'*[@key = ''' || . || ''']'"/>
    </xsl:template>

    <xsl:template match=".[contains(., '=')]" mode="step">
        <xsl:if test="position() gt 1">/</xsl:if>
        <xsl:sequence select="'*[@key = ''' || substring-before(., '=') || ''']'"/>
    </xsl:template>

    <xsl:template match=".[contains(., '[')]" mode="step">
        <xsl:if test="position() gt 1">/</xsl:if>
        <xsl:sequence select="'*[@key = ''' || substring-before(., '[') || ''']/*[' || replace(., '^[^\[]+\[([0-9]+)\]', '') || ']'"/>
    </xsl:template>

    <xsl:template match="/">
        <xsl:sequence select="transform(map {
            'source-node' : .,
            'stylesheet-node' : $stylesheet
          })?output"/>
    </xsl:template>

</xsl:stylesheet> 

https://xsltfiddle.liberty-development.net/jyyiVhv/1 我得到了想要的结果

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
    <map key="Response">
        <map key="Headers">
            <string key="server">WebSphere</string>
            <string key="Status-Code">400</string>
            <string key="Content-Type">text</string>
        </map>
        <map key="Payload">
            <map key="root">
                <array key="cars">
                    <map>
                        <string key="company">ccc1</string>
                        <string key="model">mmm1</string>
                    </map>
                    <map>
                        <string key="strength">999</string>
                        <string key="Capacity">555</string>
                    </map>
                </array>
                <array key="bikes">
                    <map>
                        <string key="company">xyz</string>
                        <string key="model">2019</string>
                    </map>
                </array>
            </map>
        </map>
    </map>
</map>

那样虽然我不得不调整包括 bikesbikes[1] 的路径。