使用 xslt 替换 xml 节点

在此 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>


慕标琳琳
浏览 61回答 1
1回答

慕容3067478

转换设计应该是从标识转换开始(请参阅&nbsp;https://www.w3.org/TR/xslt20/#element-copy&nbsp;中的“示例:标识转换”部分),然后只为要转换的节点添加模板,在 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"&nbsp; &nbsp; xmlns:xs="http://www.w3.org/2001/XMLSchema"&nbsp; &nbsp; xpath-default-namespace="http://www.example.com/something"&nbsp; &nbsp; xmlns="http://www.example.com/something"&nbsp; &nbsp; exclude-result-prefixes="#all"&nbsp; &nbsp; version="3.0">&nbsp; <xsl:mode on-no-match="shallow-copy"/>&nbsp; <xsl:template match="flow/transform">&nbsp; &nbsp; &nbsp; <message>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:apply-templates select="@*, node()"/>&nbsp; &nbsp; &nbsp; </message>&nbsp; </xsl:template></xsl:stylesheet>https://xsltfiddle.liberty-development.net/pPzifpw在 XSLT 2 中,您必须将其拼写出来:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&nbsp; &nbsp; xmlns:xs="http://www.w3.org/2001/XMLSchema"&nbsp; &nbsp; xpath-default-namespace="http://www.example.com/something"&nbsp; &nbsp; xmlns="http://www.example.com/something"&nbsp; &nbsp; exclude-result-prefixes="#all"&nbsp; &nbsp; version="2.0">&nbsp; <xsl:template match="@* | node()">&nbsp; &nbsp; &nbsp; <xsl:copy>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:apply-templates select="@*, node()"/>&nbsp; &nbsp; &nbsp; </xsl:copy>&nbsp; </xsl:template>&nbsp; <xsl:template match="flow/transform">&nbsp; &nbsp; &nbsp; <message>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:apply-templates select="@*, node()"/>&nbsp; &nbsp; &nbsp; </message>&nbsp; </xsl:template></xsl:stylesheet>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java