Navin Dalal
Navin Dalal

Reputation: 179

Extracting value from External xml based on some rule in xsl

This is input xml input.xml

    <root>
    <bodytext>
    <remotelink refptid="HKBL1.0001.lohk.CAP65">some text</remotelink>
    <remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
    </bodytext>
    </root>

This is Prop.xml

    <?xml version="1.0" encoding="utf-8"?>
    <properties>
    <code dpsi="0BZG" docid="asdww">HKBL1.0001.lohk.CAP65</code>
    <code dpsi="0BZH" docid="navin">HKBL1.0002.aohk.CAP383</code>
    <code no="3">345</code>
    </properties>

This is desired output

    <root>
    <bodytext>
    <remotelink refptid="HKBL1.0001.lohk.CAP65" dpsi="0BZG" docid="asdww">some text</remotelink>
    <remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
    </bodytext>
    </root>

IF prop.xml code/text matches remotelink/@refptid than copy attribute of prop.xml to remotelink otherwise no changes in remotelink.

This is the XSLT I have written. So far I am not getting result for unmatched condition:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlsx="http://www.stylusstudio.com/XSLT/XLSX" xmlns:spml="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:saxon="http://saxon.sf.net/" version="2.0">
    <xsl:template match="@*|node()"  name="root">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="remotelink[@service='DOC-ID']" name="t-remote">
    <xsl:variable name="refptid" select="./@refpt"/>    
    <xsl:variable name="path" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')"/>
    <xsl:for-each select="$path/properties/code">
    <xsl:choose>
    <xsl:when test="./text()=$refptid">
    <xsl:element name="remotelink">    
    <xsl:attribute name="DOC-ID" select="./@docid"/>
    <xsl:attribute name="dpsi" select="./@dpsi"/>
    <xsl:attribute name="refpt" select="$refptid"/> 
    </xsl:element>
    </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
    </xsl:choose>    
    </xsl:for-each>


    <xsl:if test="./@docrefid"/>

    </xsl:template>
    </xsl:stylesheet>

Upvotes: 1

Views: 208

Answers (2)

potame
potame

Reputation: 7905

<xsl:template match="remotelink[@service='DOC-ID']"> will never never be triggered, there's no service attribute on the <remotelink> element. Moreover, you don't retrieve the correct attribute (refpt instead of refptid)

This XSL transformation should do the job:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlsx="http://www.stylusstudio.com/XSLT/XLSX" xmlns:spml="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:saxon="http://saxon.sf.net/" version="2.0">

    <xsl:variable name="props.doc" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')/properties" />

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

    <xsl:template match="remotelink" name="t-remote">
        <xsl:variable name="refptid" select="./@refptid"/>

        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:if test="$props.doc/code[. = $refptid]">
                <xsl:apply-templates select="$props.doc/code[. = $refptid]/@*"/>                
            </xsl:if>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>

    </xsl:template>
</xsl:stylesheet>

This is the result of the transformation:

<?xml version="1.0" encoding="UTF-8"?><root>
<bodytext>
    <remotelink refptid="HKBL1.0001.lohk.CAP65" dpsi="0BZG" docid="asdww">some text</remotelink>
    <remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
</bodytext>
</root>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167506

I would simply define a global parameter or variable <xsl:variable name="path" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')"/>, then set up a key <xsl:key name="prop" match="code" use="."/>, and then use that in a template

<xsl:template match="remotelink[key('prop', @refptid, $path)]">
  <xsl:copy>
    <xsl:copy-of select="key('prop', @refptid, $path)/@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

that, together with your first template, should suffice.

Upvotes: 0

Related Questions