为了账号安全,请及时绑定邮箱和手机立即绑定

使用 xslt 替换 xml 节点

使用 xslt 替换 xml 节点

慕标琳琳 2022-09-22 19:38:19
在此 xml 中,我想用另一个节点替换该节点,但复制数据。<transfom><message>使用 xslt 是否可行,我使用 xslt 2.0 将转换节点隐藏到消息节点,但它仅适用于一个流节点。<root    xmlns="http://www.example.com/something">    <flow>        <list name="listName"/>        <router name="router"/>        <!-- I have some other tags here -->    </flow>    <flow>        <list name="listName"/>        <console name="console"/>        <!-- I have some other tags here -->    </flow>    <flow>        <payload name="example"/>        <transform name="transform">            <!-- Some DATA here --->        </transform>        <!-- I have some other tags here -->    </flow>    <flow>        <payload name="sada"/>        <transform name="transform1">            <!-- Some DATA here --->        </transform>        <!-- I have some other tags here -->        <transform name="transform2">            <!-- Some DATA here --->        </transform>    </flow></root>这些节点存在于两个节点中。有没有办法编写一个通用的XSLT,将 替换为节点,以保持节点的位置和节点内的数据。<transform><flow><transform><message>我使用了和和xpath表达式,例如前面,后面。但是它们只转换 ,但按原样复制所有其他 xml 节点。请让我知道如何解决这个问题!<xsl:for-each><xsl:when><transform name="transform node">更新这是我用来转换 xml 的样式表<xsl:stylesheet version="2.0"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <root>        <xsl:for-each select="flow">            <xsl:choose>                <xsl:when test="descendant-or-self::transform">                    <message>                        <xsl:attribute name="doc:name">                            <xsl:value-of                                        select="//transform/@name" />                        </xsl:attribute>                        <ee:message>                            <ee:set-payload>                                <xsl:value-of select="payload" />                            </ee:set-payload>                        </ee:message>                    </ee:transform>                </xsl:when>            </xsl:choose>        </xsl:for-each>    </root></xsl:stylesheet>更新 14/04/2019如何从标签更改命名空间?转换时,我有一堆命名空间要更改。如何做到这一点?<root>
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

转换设计应该是从标识转换开始(请参阅 https://www.w3.org/TR/xslt20/#element-copy 中的“示例:标识转换”部分),然后只为要转换的节点添加模板,在 XSLT 3 中,您可以使用 (https://www.w3.org/TR/xslt-30/#built-in-templates-shallow-copy) 将标识转换声明为默认处理,然后只需为元素编写一个模板:<xsl:mode on-no-match="shallow-copy"/>transform


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

    xpath-default-namespace="http://www.example.com/something"

    xmlns="http://www.example.com/something"

    exclude-result-prefixes="#all"

    version="3.0">


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


  <xsl:template match="flow/transform">

      <message>

          <xsl:apply-templates select="@*, node()"/>

      </message>

  </xsl:template>


</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/pPzifpw


在 XSLT 2 中,您必须将其拼写出来:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

    xpath-default-namespace="http://www.example.com/something"

    xmlns="http://www.example.com/something"

    exclude-result-prefixes="#all"

    version="2.0">


  <xsl:template match="@* | node()">

      <xsl:copy>

          <xsl:apply-templates select="@*, node()"/>

      </xsl:copy>

  </xsl:template>


  <xsl:template match="flow/transform">

      <message>

          <xsl:apply-templates select="@*, node()"/>

      </message>

  </xsl:template>


</xsl:stylesheet>


查看完整回答
反对 回复 2022-09-22
  • 1 回答
  • 0 关注
  • 57 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信