繁体   English   中英

如何复制所有消息并使用xslt添加一些字段

[英]how to copy all message and add some field with xslt

我需要复制整个xml消息并添加一些字段,但是在应用xslt时,仅复制我的值而不是标签的名称,请您能帮我吗

这是输入XML

<Xmlroot>
    <headerIn>
        <field1>hello</field1>
        <field2>world</field2>
    </headerIn>
</Xmlroot>

我需要这个回应

<Xmlroot>
    <headerIn>
        <field1>hello</field1>
        <field2>world</field2>
        <other>nice</other>
    </headerIn>
</Xmlroot>

我有这个xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.example.org/tns" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
  <xsl:output method="xml" omit-xml-declaration="yes"/>


    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="headerIn">
    <headerIn>
        <xsl:element name="{@name}">
            <xsl:value-of select="."/>
        </xsl:element>
    </headerIn>

    </xsl:template>

</xsl:stylesheet>

我得到这个输出

<Xmlroot>
    <headerIn xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.example.org/tns">hello
    world
    </headerIn>
</Xmlroot>

以下是用于转换值和名称的有效XSLT。 然后,您可以添加要创建的其他输出。 如示例中所示,我添加了一个附加元素<other> 保留元素名称的关键是使用{local-name()}

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="2.0">
<xsl:output method="xml"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="headerIn">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates/>
      <xsl:element name="other">nice</xsl:element>
  </xsl:element>
  </xsl:template>
</xsl:stylesheet>

尝试使用此XSL模板复制所有节点,并在适当的位置简单地添加元素<other>nice</other>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.example.org/tns" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
  <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="headerIn">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        <other>nice</other>  <!-- replace this line with whatever element you want to add -->
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM