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>
rmarq423
  • 195
  • 1
  • 8
  • possible duplicate of [How to remove elements from xml using xslt with stylesheet and xsltproc?](http://stackoverflow.com/questions/321860/how-to-remove-elements-from-xml-using-xslt-with-stylesheet-and-xsltproc) – DanMan Feb 13 '15 at 19:31

2 Answers2

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
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>
rmarq423
  • 195
  • 1
  • 8