1

I'd like to eliminate injecting duplicate nodes if they already exist in the XML source file. My current code correctly inserts what I want, but does not check to see if the node already exists.

Here is my original XML file I need to manipulate:

    <?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <fullName>Data_Check_Comments__c</fullName>
        <description>Checking Data for Company</description>
        <label>Data Check Comments</label>
    </fields>
    <fields>
        <fullName>My_Test_Obj__c</fullName>
        <description>General info about the test object.</description>
        <inlineHelpText>This is simply a test object.</inlineHelpText>
        <label>My Test Obj</label>
    </fields>
</CustomObject>

Here is my desired output XML:

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <inlineHelpText>fields</inlineHelpText>
        <fullName>Data_Check_Comments__c</fullName>
        <description>Checking Data for Company</description>
        <label>Data Check Comments</label>
    </fields>
    <fields>
        <!--***I don't want this duplicate*** inlineHelpText xmlns="">fields</inlineHelpText-->
        <fullName>My_Test_Obj__c</fullName>
        <description>General info about the test object.</description>
        <inlineHelpText>This is simply a test object.</inlineHelpText>
        <label>My Test Obj</label>
    </fields>
</CustomObject>

Finally here is my current xlst:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:a="http://soap.sforce.com/2006/04/metadata">

  <xsl:template match="a:CustomObject/*">
    <xsl:copy>
      <xsl:element name="inlineHelpText">
        <xsl:value-of select="name(.)"/>
      </xsl:element>

      <xsl:call-template name="copy-children"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="copy-children">
    <xsl:copy-of select="./*"/>
  </xsl:template>

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

2 Answers 2

1

It's difficult to understand what in your question is given and what's just an example. Would something like this work for you?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:a="http://soap.sforce.com/2006/04/metadata"
exclude-result-prefixes="a">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="a:fields[not(a:inlineHelpText)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <inlineHelpText xmlns="http://soap.sforce.com/2006/04/metadata">fields</inlineHelpText>
    </xsl:copy>
</xsl:template>

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

2 Comments

Please let me know what is unclear in the question and I'll make a point to change it. I'll clean up the original question now.
@rmarq423 Why don't you let me know what in my answer does not fit your situation.
1

Actually I figured out the problem. Sorry if the question wasn't clear. By using an if and the syntax below, I was able to disregard child nodes if they already existed. Thanks.

<xsl:if test="not(descendant::*[local-name()='inlineHelpText'])">
     <xsl:element name="inlineHelpText">
        <xsl:value-of select="name(.)"/>
     </xsl:element>
</xsl:if>

1 Comment

I doubt that solves your problem, because the new element that you insert is in no namespace.

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.