Can XML Refactoring convert an element to an attribute?

Questions about XML that are not covered by the other forums should go here.
binar
Posts: 18
Joined: Wed Apr 10, 2019 7:43 pm

Can XML Refactoring convert an element to an attribute?

Post by binar » Fri Jun 07, 2019 7:00 am

Greetings,
I need to convert S1000D Issue 4.0 XML files over to Issue 4.1. which is a very big project. The two code examples shown below show a coding change that has taken place between the two Issue releases. My hope is XML Refactoring can automatically perform for me this change accross many XML files and save me the work of doing it manually.

The change is as follows. In the Example #1 code the <manufacturerCode>777</manufacturerCode> tag and the <partNumber>1010</partNumber> tag exist as elements. However, in Example #2 both of these elements have been changed to attributes now and also have been renamed to manufacturerCodeValue="777" and partNumberValue="1010". Additionally, both newly renamed attributes now exist inside the <partRef> tag. Can XML Refactoring perform an operation that involves changing the two mentioned elements to the two mentioned attribute names and relocating the new renamed attributes into the <partRef> tag as shown in Example #2? I would really like to know if such an XML Refactoring operation is possible because if it is indeed possible it will help me out a lot. Thank you for your help.

Example #1: Issue 4.0 Bicycle

Code: Select all

<catalogSeqNumber catalogSeqNumberValue="D00000000A001 " indenture="2">
    <itemSequenceNumber itemSeqNumberValue="00A">
        <reasonForSelection reasonForSelectionValue="1"/>
        <quantityPerNextHigherAssy>1</quantityPerNextHigherAssy>
        <manufacturerCode>KZ777</manufacturerCode>
        <partNumber>LRU1010</partNumber>
        <partIdentSegment>
            <descrForPart>Light, sub-assembly front</descrForPart>
            <unitOfIssue>EA</unitOfIssue>
            <specialStorage>1</specialStorage>
        </partIdentSegment>
        <partLocationSegment>
            <descrForLocation>FRONT</descrForLocation>
        </partLocationSegment>
        <locationRcmdSegment>
            <locationRcmd>
                <service>UKA</service>
                <sourceMaintRecoverability>PAODF</sourceMaintRecoverability>
            </locationRcmd>
        </locationRcmdSegment>
    </itemSequenceNumber>
</catalogSeqNumber>
Example #2: Issue 4.1 Bicycle

Code: Select all

<catalogSeqNumber indenture="3" systemCode="D00" subSystemCode="0" subSubSystemCode="0" assyCode="00" figureNumber="01" item="002">
     <itemSeqNumber itemSeqNumberValue="00A">
              <reasonForSelection reasonForSelectionValue="1"/>
              <quantityPerNextHigherAssy>1</quantityPerNextHigherAssy>
              <partRef manufacturerCodeValue="KZ777" partNumberValue="LRU1011"/>
              <partLocationSegment/>
                    <locationRcmdSegment>
                        <locationRcmd>
                            <service>UKA</service>
                            <sourceMaintRecoverability>PAFFD</sourceMaintRecoverability>
                        </locationRcmd>
                    </locationRcmdSegment>
      </itemSeqNumber>
</catalogSeqNumber>

Radu
Posts: 6541
Joined: Fri Jul 09, 2004 5:18 pm

Re: Can XML Refactoring convert an element to an attribute?

Post by Radu » Fri Jun 07, 2019 7:50 am

Hi,

The XSLT stylesheet for a custom XML refactoring operation could look something like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="manufacturerCode">
        <xsl:element name="partRef">
            <xsl:attribute name="manufacturerCodeValue"><xsl:value-of select="."/></xsl:attribute>
            <xsl:if test="following-sibling::*[1][local-name()='partNumber']">
                <xsl:attribute name="partNumber"><xsl:value-of select="following-sibling::*[1][local-name()='partNumber']"/></xsl:attribute>
            </xsl:if>
        </xsl:element>
    </xsl:template>
    
    <!-- Ignore, will be output as attribute in another element -->
    <xsl:template match="partNumber"/>
</xsl:stylesheet>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

binar
Posts: 18
Joined: Wed Apr 10, 2019 7:43 pm

Re: Can XML Refactoring convert an element to an attribute?

Post by binar » Tue Jun 11, 2019 5:49 am

Radu,
Thank you very much for your XSL template. This one in my opinion is the most amazing one because of how quickly it performs numerous changes. If I had to manually edit the code so it can end up looking like the example below would have been a very tedious task to perform considering we are talking about a bunch of data modules.

Code: Select all

<partRef manufacturerCodeValue="KZ777" partNumberValue="LRU1011"/>
In short, this XSL template was a big time saver and again I thank you very much for it.

Post Reply