Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 从foreach中排除指定的元素_Json_Xml_Xslt_Xslt 3.0 - Fatal编程技术网

Json 从foreach中排除指定的元素

Json 从foreach中排除指定的元素,json,xml,xslt,xslt-3.0,Json,Xml,Xslt,Xslt 3.0,我希望在for-each期间使用导入的JSON数据中的结果映射排除一些元素。 我怀疑我正在运行的测试在将XML作为源代码运行时可能会起作用,但在将JSON作为源代码运行时可能不会起作用 JSON数据: <data> { "storage": { "pencils": 12, "milk": 8, "rulers": 4 } } </data> <?xml

我希望在for-each期间使用导入的JSON数据中的结果映射排除一些元素。 我怀疑我正在运行的测试在将XML作为源代码运行时可能会起作用,但在将JSON作为源代码运行时可能不会起作用

JSON数据:

<data>
{
  "storage": {
    "pencils": 12,
    "milk": 8,
    "rulers": 4
  }
}
</data>
<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform
  version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:storage="http://www.example.com/1"
  xmlns:office="http://www.example.com/2"
  xmlns:item="http://www.example.com/3"
  expand-text="yes">

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

  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <storage:one>
      <xsl:apply-templates select="json-to-xml(.)"/>
    </storage:one>
  </xsl:template>

  <xsl:template match="*[@key='storage']">

    <!-- Startbase -->
    <!-- <xsl:for-each select="*"> -->

    <!-- Test[2] -->
    <xsl:for-each select="*[local-name() != 'milk']">
    <!-- Test[3] -->
      <xsl:for-each select="*[not(self::*/@key=milk)]">

      <xsl:element name="item:{@key}">
        <xsl:attribute name="office">plant-1</xsl:attribute>
        <xsl:value-of select="text()"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:milk office="plant-1">8</item:milk>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>

{
“储存”:{
“铅笔”:12,
“牛奶”:8,
“统治者”:4
}
}
**

XSL:

<data>
{
  "storage": {
    "pencils": 12,
    "milk": 8,
    "rulers": 4
  }
}
</data>
<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform
  version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:storage="http://www.example.com/1"
  xmlns:office="http://www.example.com/2"
  xmlns:item="http://www.example.com/3"
  expand-text="yes">

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

  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <storage:one>
      <xsl:apply-templates select="json-to-xml(.)"/>
    </storage:one>
  </xsl:template>

  <xsl:template match="*[@key='storage']">

    <!-- Startbase -->
    <!-- <xsl:for-each select="*"> -->

    <!-- Test[2] -->
    <xsl:for-each select="*[local-name() != 'milk']">
    <!-- Test[3] -->
      <xsl:for-each select="*[not(self::*/@key=milk)]">

      <xsl:element name="item:{@key}">
        <xsl:attribute name="office">plant-1</xsl:attribute>
        <xsl:value-of select="text()"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:milk office="plant-1">8</item:milk>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>

植物-1
结果:

<data>
{
  "storage": {
    "pencils": 12,
    "milk": 8,
    "rulers": 4
  }
}
</data>
<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform
  version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:storage="http://www.example.com/1"
  xmlns:office="http://www.example.com/2"
  xmlns:item="http://www.example.com/3"
  expand-text="yes">

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

  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <storage:one>
      <xsl:apply-templates select="json-to-xml(.)"/>
    </storage:one>
  </xsl:template>

  <xsl:template match="*[@key='storage']">

    <!-- Startbase -->
    <!-- <xsl:for-each select="*"> -->

    <!-- Test[2] -->
    <xsl:for-each select="*[local-name() != 'milk']">
    <!-- Test[3] -->
      <xsl:for-each select="*[not(self::*/@key=milk)]">

      <xsl:element name="item:{@key}">
        <xsl:attribute name="office">plant-1</xsl:attribute>
        <xsl:value-of select="text()"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:milk office="plant-1">8</item:milk>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>

12
8.
4.
想要的结果:

<data>
{
  "storage": {
    "pencils": 12,
    "milk": 8,
    "rulers": 4
  }
}
</data>
<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform
  version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:storage="http://www.example.com/1"
  xmlns:office="http://www.example.com/2"
  xmlns:item="http://www.example.com/3"
  expand-text="yes">

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

  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <storage:one>
      <xsl:apply-templates select="json-to-xml(.)"/>
    </storage:one>
  </xsl:template>

  <xsl:template match="*[@key='storage']">

    <!-- Startbase -->
    <!-- <xsl:for-each select="*"> -->

    <!-- Test[2] -->
    <xsl:for-each select="*[local-name() != 'milk']">
    <!-- Test[3] -->
      <xsl:for-each select="*[not(self::*/@key=milk)]">

      <xsl:element name="item:{@key}">
        <xsl:attribute name="office">plant-1</xsl:attribute>
        <xsl:value-of select="text()"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:milk office="plant-1">8</item:milk>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>
<?xml version="1.0" encoding="UTF-8"?>
<storage:one xmlns:item="http://www.example.com/3"
             xmlns:office="http://www.example.com/2"
             xmlns:storage="http://www.example.com/1">
   <item:pencils office="plant-1">12</item:pencils>
   <item:rulers office="plant-1">4</item:rulers>
</storage:one>

12
4.

将每个元素的对应值调整为以下值将排除指定的元素:

<xsl:for-each select="*[not(@key='milk')]">