简体   繁体   中英

how to copy all message and add some field with xslt

I need to copy the entire xml message and add a few fields, but at the moment of applying the xslt only copy me the values and not the name of the tag, can you help me please

this is de input xml

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

i need this response

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

i have this 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>

and I obtain this output

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

Below is a valid XSLT for transforming the values and the name. You can then add the additional output you want to create. I have added one additional element <other> as you showed in your example. The key for preserving the element name is use of {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>

Try using this XSL-template which copies all nodes and simply adds the element <other>nice</other> at the appropriate place:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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