简体   繁体   中英

How to replace a character in a node name of XML using XSLT

I need to convert a XML using a XSLT mapping and I have no idea how to do this...... can you help me

My source XML:

<?xml version="1.0" encoding="UTF-8"?>
<order-header>
    <id type="integer">1</id>
    <created-at type="dateTime">2020-01-25T18:59:02-03:00</created-at>
    <updated-at type="dateTime">2020-04-23T15:28:13-03:00</updated-at>
    <po-number>1</po-number>
    <price-hidden type="boolean">false</price-hidden>
    <acknowledged-flag type="boolean">false</acknowledged-flag>
    <acknowledged-at nil="true"/>
    <status>issued</status>
    <transmission-status>sent_via_email</transmission-status>
    <version type="integer">1</version>
    <internal-revision type="integer">3</internal-revision>
    <exported type="boolean">false</exported>
    <last-exported-at nil="true"/>
    <payment-method>invoice</payment-method>
    <ship-to-attention>10422282000179</ship-to-attention>
    <coupa-accelerate-status nil="true"/>
    <change-type>revision</change-type>
    <transmission-method-override>supplier_default</transmission-method-override>
    <transmission-emails></transmission-emails>
</order-header>

And I need this result:

<?xml version="1.0" encoding="UTF_8"?>
<order_header>
    <id type="integer">1</id>
    <created_at type="dateTime">2020-01-25T18:59:02-03:00</created_at>
    <updated_at type="dateTime">2020-04-23T15:28:13-03:00</updated_at>
    <po_number>1</po_number>
    <price_hidden type="boolean">false</price_hidden>
    <acknowledged_flag type="boolean">false</acknowledged_flag>
    <acknowledged_at nil="true"/>
    <status>issued</status>
    <transmission_status>sent_via_email</transmission_status>
    <version type="integer">1</version>
    <internal_revision type="integer">3</internal_revision>
    <exported type="boolean">false</exported>
    <last_exported_at nil="true"/>
    <payment_method>invoice</payment_method>
    <ship_to_attention>10422282000179</ship_to_attention>
    <coupa_accelerate_status nil="true"/>
    <change_type>revision</change_type>
    <transmission_method_override>supplier_default</transmission_method_override>
    <transmission_emails></transmission_emails>
</order_header>

I already read a lot of articles but it doesn't work

Tks in advance

Use a template

  <xsl:template match="*">
      <xsl:element name="{translate(name(), '-', '_')}">
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

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

If I get this correctly, you want to replace all - in node names by _.

Here's how you could do it:

<?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"
    version="1.0">

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

  <xsl:template match="*[contains(local-name(.),'-')]">
    <xsl:element name="{translate(local-name(),'-','_')}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>    
    </xsl:element>
  </xsl:template>

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

</xsl:stylesheet>

See it working here: https://xsltfiddle.liberty-development.net/3MvmXiM

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