xml - 如何使用 xslt 在特定 xml 上运行 xpath

标签 xml xslt xpath

这个问题与我的问题之一几乎相同 edit 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 在给定的 xpaths 上更改我的 xml。查看此位置的代码 xsltfiddle.liberty-development.net

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

最佳答案

得到像这样的路径

/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 推断有一个数组成员参与。

关于xml - 如何使用 xslt 在特定 xml 上运行 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50506834/

相关文章:

c# - XmlDocument CreateElement 在前缀元素下没有 xmlns

XSLT - 在谓词过滤器中,为什么有时必须使用 XSLT current() 函数而不是 XPath 上下文节点点运算符?

xslt - 强制 HTML Tidy 输出 XML(而不是 XHTML),或者强制 XSLTproc 解析 XHTML 文件

java - 修复XPath表达式中的绝对路径,以便它们可在另一个文档的上下文中使用

Python ElementTree - 按顺序遍历子节点和文本

xml - Xpath 使用谓词在数据中匹配带有斜线的节点

xml - 获取 XPATH 中属性的字符串值

c# - Xml 序列化 - 忽略父节点

java - 如何从解压文件中获取 XML 值

xml - 如何使用 XPath 选择最后 N 个元素?