【问题标题】:How to run xpath on specific xml using xslt如何使用 xslt 在特定 xml 上运行 xpath
【发布时间】:2018-11-03 12:47:41
【问题描述】:

这个问题和我在edit specific xml using xpathedit specific xml using xpath的问题几乎一样

但现在问题不同了。 xml 结构现在略有不同。先前问题的解决方案在这里不起作用。

我有以下 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
    <map key="Request">
        <map key="Headers">
            <string key="Accept">application/json</string>
            <string key="Content-Type">application/json</string>
        </map>
        <map key="Payload">
            <map key="root">
                <array key="req1">
                    <string>puneet</string>
                    <string>taneja</string>
                </array>
                <array key="req2">
                    <string>ratan</string>
                </array>
            </map>
        </map>
    </map>
</map>

以下是 XPATH

/Request/Headers/Accept=app
/Request/Headers/Content-Type=json
/Request/Payload/root/req1[2]=singh
/Request/Payload/root/req1[1]=pradeep
/Request/Payload/root/req2=suman

我想要更新结果 xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
    <map key="Request">
        <map key="Headers">
            <string key="Accept">application/json</string>
            <string key="Content-Type">application/json</string>
        </map>
        <map key="Payload">
            <map key="root">
                <array key="req1">
                    <string>pradeep</string>
                    <string>singh</string>
                </array>
                <array key="req2">
                    <string>suman</string>
                </array>
            </map>
        </map>
    </map>
</map>

基本上,我想使用 xslt 在给定的 xpath 上更改我的 xml。查看此位置的代码xsltfiddle.liberty-development.net

对于这种情况,在相同的早期解决方案中进行更新也会对我有所帮助。

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    获取路径

    /Request/Payload/root/req1[2]=singh
    /Request/Payload/root/req1[1]=pradeep
    

    工作中可以添加模板

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

    这样的代码

       <xsl:template match="*[@key = 'Request']/*[@key = 'Payload']/*[@key = 'root']/*[@key = 'req1']/*[1]">
          <xsl:copy>
             <xsl:copy-of select="@*"/>pradeep</xsl:copy>
       </xsl:template>
       <xsl:template match="*[@key = 'Request']/*[@key = 'Payload']/*[@key = 'root']/*[@key = 'req1']/*[2]">
          <xsl:copy>
             <xsl:copy-of select="@*"/>singh</xsl:copy>
       </xsl:template>
    

    正在生成,请参阅https://xsltfiddle.liberty-development.net/bdxtpY/1。

    我目前不确定如何从/Request/Payload/root/req2=suman 推断出涉及到一个数组成员。

    【讨论】:

    • 新模板解决了这个问题。再次感谢马丁 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多