0

I have problem with the xslt which is trying to get the value of a nested element when id of file1 matches with the id in file2, then, the value from file2 should be returned in output.xml

I have two xmls namely file1.xml and file2.xml, as given below: Please help in resolving it ASAP

file1.xml

<CustomerSegmentLists>
  <CustomerSegmentList>
    <Id>1730004b-4749-4d39-b257-fc25ca5698ec</Id>
    <CustomerSegment 
        id="3c3b6cda-2d59-4e81-a3b4-ebe54fb7578d"
        core_data_overridden="false" action="noChange">
      <Name>
        <Value locale="en">CSBusniessConfidenceLevel</Value>
      </Name>
      <CustomerSegmentVersions>
        <CustomerSegmentVersion 
            effective="2014-06-02T00:00:00"
            expiration="NONE" action="noChange">
          <Segment 
              id="3a2906f3-40b1-483b-84bf-e816f4fe53ee"
              type="FieldListInstanceField">
            <CustomerSegmentType 
                element_id="841721af-51ae-4b44-b5e8-6dbff1cfd34e">
              <Field 
                  name="Confidence" id="555c4bcc-f0f5-4ab4-b2fa-439b0f5760f0"
                  type="ElementaryField" value="LOW" />
            </CustomerSegmentType>
          </Segment>
        </CustomerSegmentVersion>
      </CustomerSegmentVersions>
    </CustomerSegment>
  </CustomerSegmentList>
</CustomerSegmentLists>

file2.xml

<CustomerSegmentTypeLists>
  <CustomerSegmentTypeList>
    <Id>bb327a99-6a88-4772-84f9-aea40b4fc878</Id>
    <CustomerSegmentType id="841721af-51ae-4b44-b5e8-6dbff1cfd34e"
                         core_data_overridden="false" action="noChange">
      <Name>
        <Value locale="en">BusniessConfidenceLevel</Value>
      </Name>
      <CustomerSegmentTypeVersions>
        <CustomerSegmentTypeVersion effective="2014-05-20T00:00:00"
                                    expiration="NONE" action="noChange">
          <SegmentType id="f4c2fe74-beba-4c6c-b819-ee3edb526f0a"
                       type="FieldListField">
            <View><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
            <view version="1" />
            ]]></View>
            <ElementaryField 
                elementary_type_id="APC_String_Elementary_Type"
                elementary_type_name="String" unique="false" allow_repetitions="false"
                id="555c4bcc-f0f5-4ab4-b2fa-439b0f5760f0" name="Confidence"
                min_occurrences="1" max_occurrences="1">
              <DisplayName>
                <Value locale="en">Confidence</Value>
              </DisplayName>
            </ElementaryField>
          </SegmentType>
        </CustomerSegmentTypeVersion>
      </CustomerSegmentTypeVersions>
    </CustomerSegmentType>
  </CustomerSegmentTypeList>
</CustomerSegmentTypeLists>

There are arounnd 12 similar tags in each of the xml. I have an xslt as given below:

myxslt.xslt

<xsl:element name="{fn:concat($nodeName, 's')}">
  <xsl:for-each select="./*">
    <xsl:element name="{$nodeName}">
      <xsl:for-each select="$csfile//CustomerSegmentType">
        <xsl:variable name="csid" select="@element_id" />
        <xsl:for-each select="$cstfile//CustomerSegmentType">
          <xsl:variable name="cstid" select="@id" />
          <xsl:variable name="value" select="Name/Value" />
          <xsl:choose>
            <xsl:when test="$csid =$cstid">
              CSTVALUE ::
              <xsl:value-of select="$value" />
            </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:for-each select="./*">
        <xsl:apply-templates select="." />
      </xsl:for-each>
    </xsl:element>
  </xsl:for-each>
</xsl:element>

Now, ideally, the output should be 12 matching with their 12 matches, but the output should have one matching value, while it is returning all 12 values with each tag, which should be corrected.

Output is given as below.

<customerSegment guid="3c3b6cda-2d59-4e81-a3b4-ebe54fb7578d">
  <name>CSBusniessConfidenceLevel</name>
  <hierarchyPath />
  <segments>
    <segment>
      CSTVALUE :: AccountType 
      CSTVALUE :: ActivationBetween 
      CSTVALUE :: AllStarTier 
      CSTVALUE :: BusniessConfidenceLevel 
      CSTVALUE :: Certificate 
      CSTVALUE :: ClaimCode 
      CSTVALUE :: CreditQualification
      CSTVALUE :: EstimatedViewingOccupancyTier 
      CSTVALUE :: FireOccupancyCodeTier 
      CSTVALUE :: LineOfService 
      CSTVALUE :: NoLineOfSight 
      CSTVALUE :: TempStarTier
      <field>LOW</field>
    </segment>
  </segments>
</customerSegment>

I want it to be only single matching value (i.e. CSTVALUE :: BusniessConfidenceLevel and not rest 11) and not all the 12 values for each node. Please help it is urgent.

  • It is giving me all the 12 CSTValues per tag, I want only single matching value per tag – Hilal Jun 23 '14 at 6:47
1

I assume that you have to rearrange the creation of the <segment> node and your outer loop over the <CustomerSegmentType> nodes:

<xsl:element name="{fn:concat($nodeName, 's')}">
  <xsl:for-each select="./*">
    <xsl:for-each select="$csfile//CustomerSegmentType">
      <xsl:variable name="csid" select="@element_id" />
      <xsl:element name="{$nodeName}">
        <xsl:for-each select="$cstfile//CustomerSegmentType">
          <xsl:variable name="cstid" select="@id" />
          <xsl:variable name="value" select="Name/Value" />
          <xsl:choose>
            <xsl:when test="$csid =$cstid">
              CSTVALUE ::
              <xsl:value-of select="$value" />
            </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:for-each select="./*">
        <xsl:apply-templates select="." />
      </xsl:for-each>
    </xsl:for-each>
  </xsl:element>
</xsl:element>
| improve this answer | |
  • This is giving the same output as above, it should have output of segment tag as given below: <segment>CSTVALUE:: BusniessConfidenceLevel</segment> ONly single segment not all the 12 – Hilal Jun 23 '14 at 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.