程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了查询具有不同路径的相同元素名称大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决查询具有不同路径的相同元素名称?

开发过程中遇到查询具有不同路径的相同元素名称的问题如何解决?下面主要结合日常开发的经验,给出你关于查询具有不同路径的相同元素名称的解决方法建议,希望对你解决查询具有不同路径的相同元素名称有所启发或帮助;

我尝试在导入的 JsON 文件中使用唯一的元素名称,但有时我会使用层次结构,其中元素名称相同但元素位于同一地图中的不同位置。

我得到了想要的结果,但想知道是否有更好的方法来查询地图以查找具有相同元素名称但具有特定位置的节点。

查询不需要在同一个模板或“for-each”中。查询可以单独触发。

数据:

<data>
{
  "store1": {
    "pencils": 2
  },"store2": {
    "pencils": 5
  }
}
</data>

XSL:

<?xml version="1.0" enCoding="UTF-8"?>

<xsl:transform version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map"
  xmlns:array="http://www.w3.org/2005/xpath-functions/array"
  exclude-result-prefixes="fn array map"
  expand-text="yes"
>

  <xsl:output method="xml" indent="yes"/>
  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JsON to XML -->

  <xsl:template match="data">
      <store>
        <xsl:apply-templates SELEct="Json-to-xml(.)/*"/>
      </store>
  </xsl:template>

  <xsl:template match="*[@key='store1']">
    <xsl:copy-of SELEct="*[@key='pencils']"></xsl:copy-of>
  </xsl:template>

  <xsl:template match="*[@key='store2']">
    <xsl:copy-of SELEct="*[@key='pencils']"></xsl:copy-of>
  </xsl:template>
</xsl:transform>

结果:

<?xml version="1.0" enCoding="UTF-8"?>
<store xmlns="http://www.w3.org/1999/xhtml">
   <number xmlns="http://www.w3.org/2005/xpath-functions" key="pencils">2</number>
   <number xmlns="http://www.w3.org/2005/xpath-functions" key="pencils">5</number>
</store>

解决方法

你的意思是这样的吗?:

<xsl:template match="*[*[@key='pencils']]">
    <xsl:copy-of SELEct="*[@key='pencils']"/>
</xsl:template>

大佬总结

以上是大佬教程为你收集整理的查询具有不同路径的相同元素名称全部内容,希望文章能够帮你解决查询具有不同路径的相同元素名称所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。