Arrays 如何使用XSL 3.0在xml映射中添加/删除条目

Arrays 如何使用XSL 3.0在xml映射中添加/删除条目,arrays,dictionary,xslt,Arrays,Dictionary,Xslt,为了在1001之后将abc添加到映射中,xsl 3.0的方式是什么? 如何删除数组元素,例如1050.0? 谢谢 > 70805774 1001 1004 288 1050 324 需要少量更改的XML到XML转换通常是以标识转换为基础模板(在XSLT 3中声明为)完成的,然后为要进行的更改添加模板,例如添加元素 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:x

为了在
1001
之后将
abc
添加到映射中,xsl 3.0的方式是什么? 如何删除数组元素,例如
1050.0
? 谢谢


>
70805774
1001
1004
288
1050
324

需要少量更改的XML到XML转换通常是以标识转换为基础模板(在XSLT 3中声明为
)完成的,然后为要进行的更改添加模板,例如添加元素

<?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"
    xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
    xmlns="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="#all"
    version="3.0">

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

  <xsl:template match="string[@key = 'value'][. = '1001']">
      <xsl:next-match/>
      <string key="name">abc</string>
  </xsl:template>
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
</xsl:stylesheet>

如果您想回到XML表示,可以使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="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="#all"
    version="3.0">
  
  <xsl:template match="/">
    <xsl:sequence
      select="let $map := . => xml-to-json() => parse-json(),
                  $content := $map?content,
                  $contentMap := $content?1,
                  $newContentMap := $contentMap => map:put('name', 'abc'),
                  $newContentMap := $newContentMap => map:put('position', array:remove($contentMap?position, 3)),
                  $map := $map => map:put('content', array:put($content, 1, $newContentMap))
              return $map => serialize(map { 'method' : 'json' }) => json-to-xml()"/>
  </xsl:template>

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

XDM映射未定义/保留“对象”/“映射”项的顺序。

请重试

<xsl:template match="string[key='value']">
   <xsl:copy-of select="."/>
   <string key="value">1001</string>
</xsl:template>

<xsl:template match="array/number[3]"/>

1001

假设名称空间声明正确。

使用XML表示,只需在该示例中添加一个元素并删除一个元素。你试过什么吗?你能给我们看看你的努力吗?我认为你的
数组
不能包含一个带键的
。它们对我来说都很好。我决定赞成第一种方法。我省略了match中的value
[.='1001']
部分,因为它是动态变化的。通过使用
xsl:comments
记录中间步骤,我已经完成了这两个示例。非常感谢。谢谢,但是StackOverflow的惯例是点击问题旁边的勾号/复选标记,将答案标记为已接受-对于搜索网站的人来说,这比“谢谢”评论更好。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="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="#all"
    version="3.0">
  
  <xsl:template match="/">
    <xsl:sequence
      select="let $map := . => xml-to-json() => parse-json(),
                  $content := $map?content,
                  $contentMap := $content?1,
                  $newContentMap := $contentMap => map:put('name', 'abc'),
                  $newContentMap := $newContentMap => map:put('position', array:remove($contentMap?position, 3))
              return $map => map:put('content', array:put($content, 1, $newContentMap))"/>
  </xsl:template>

  <xsl:output method="json" indent="yes"/>
  
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="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="#all"
    version="3.0">
  
  <xsl:template match="/">
    <xsl:sequence
      select="let $map := . => xml-to-json() => parse-json(),
                  $content := $map?content,
                  $contentMap := $content?1,
                  $newContentMap := $contentMap => map:put('name', 'abc'),
                  $newContentMap := $newContentMap => map:put('position', array:remove($contentMap?position, 3)),
                  $map := $map => map:put('content', array:put($content, 1, $newContentMap))
              return $map => serialize(map { 'method' : 'json' }) => json-to-xml()"/>
  </xsl:template>

  <xsl:output method="xml" indent="yes"/>
  
</xsl:stylesheet>
<xsl:template match="string[key='value']">
   <xsl:copy-of select="."/>
   <string key="value">1001</string>
</xsl:template>

<xsl:template match="array/number[3]"/>