0

i'm stucked. Please help me with a little problem.

I have to change just two specific lines in XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<max:PublishTP_WORKORDER xmlns:max="http://www.ibm.com/maximo" creationDateTime="2014-04-11T10:43:51+04:00" transLanguage="RU" baseLanguage="EN" messageID="1397198631936413520" maximoVersion="7 5 20130829-1209 V7510--1" event="1">
  <TP_WORKORDERSet xmlns="http://www.ibm.com/maximo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <WORKORDER action="Replace">
      <ACTCOST xsi:nil="true"/>
      <ACTFINISH xsi:nil="true"/>
      <ACTINTLABCOST>0.0</ACTINTLABCOST>
      <ACTINTLABHRS>0.0</ACTINTLABHRS>
      <ACTLABCOST>0.0</ACTLABCOST>
      <ACTLABHRS>0.0</ACTLABHRS>
      <ACTMATCOST>0.0</ACTMATCOST>
      <ACTOUTLABCOST>0.0</ACTOUTLABCOST>
      <ACTOUTLABHRS>0.0</ACTOUTLABHRS>
      <ACTSERVCOST>0.0</ACTSERVCOST>
      <ACTSTART>2013-11-08T12:03:26+04:00</ACTSTART>
      <ACTTOOLCOST>0.0</ACTTOOLCOST>
      <ADDRESS/>
      <AMCREW/>
      <AMS>0</AMS>
      <AOS>0</AOS>
...........................
      <WORKORDERID>10</WORKORDERID>
      <WORKPACKMTLSTATUS/>
      <WORKTYPE/>
      <WOSEQUENCE xsi:nil="true"/>
    </WORKORDER>
  </TP_WORKORDERSet>
</max:PublishTP_WORKORDER>

I need to replace "PublishTP_WORKORDER" with "Create_WORKORDER", both open and close tags.

It works fine with:

 <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:max="http://www.ibm.com/maximo" version="1.0">
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/max:PublishTP_WORKORDER">
  <xsl:element name="max:CreateTP_WORKORDER">
  <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

But in XML file it could be "PublishTP_WORKORDER2" or "PublishTP_WORKORDER3" and so on. It should be changed to "CreateTP_WORKORDER2", "CreateTP_WORKORDER3" etc And this XSLT scheme stops working. It's just doesn't recognize strings with added numeric symbols. How could i turn it out? Thanks in advance.

8
  • is that your entire XML input? If so can you not just match on /? Commented Apr 11, 2014 at 12:06
  • Nope, enitre XML is big enough. I've tried to delete this "/", but the string is still "PublishTP_WORKORDER2" Commented Apr 11, 2014 at 12:11
  • Okay, can you update your xml example with a bit more XML? I think I know what you'd like to do but it's infinitely harder in XSLT as opposed to say php. Commented Apr 11, 2014 at 12:16
  • Instead of listing all the things you cannot rely upon being there, state the one thing that you know for sure you can hang onto. As an aside, XSLT is a custom job; if the source XML is constantly changing, then eventually the fixed XSLT is going to become obsolete. Commented Apr 11, 2014 at 12:35
  • Updated. In the XML body just list of attributes. It works okay, but only if there's no other chars after or before "max:PublishTP_WORKORDER", but it could be Commented Apr 11, 2014 at 12:36

1 Answer 1

2

It's always root element

Then how about:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:max="http://www.ibm.com/maximo">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<xsl:template match="/*">
    <xsl:variable name="suffix" select="substring-after(local-name(), 'PublishTP_WORKORDER')" />
    <xsl:element name="max:CreateTP_WORKORDER{$suffix}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.