Comparing node values and writing the value of another node based on that comparison with XSLT



I apologize if this seems obtuse or oversimplified. I'm certainly no expert with XSLT.


I have two XMLs:


XML #1



<simpletable>

<strow>
<stentry props="part-name">This is a test</stentry>
<stentry props="part-number">1008590-00-C</stentry>
</strow>
<strow>
<stentry props="part-name">Another test</stentry>
<stentry props="part-number">1008590-00-D</stentry>
</strow>
<strow>
<stentry props="part-name">Still another test</stentry>
<stentry props="part-number">1030348-00-A</stentry>
</strow>

</simpletable>


XML #2



<simpletable relcolwidth="5* 40* 15* 5* 35*">
<sthead translate="yes">
<stentry>No.</stentry>
<stentry>Part Description</stentry>
<stentry>Part Number</stentry>
<stentry>Qty</stentry>
<stentry>Comments</stentry>
</sthead>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="no" props="part-name">P​L​A​S​T​I​C​S​ ​S​U​B​ ​A​S​Y​ ​-​ ​F​R​O​N​T​ ​F​A​S​C​I​A​ </stentry>
<stentry translate="no" props="part-number">1008590-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="no" props="part-name">P​L​A​S​T​I​C​S​ ​S​U​B​ ​A​S​Y​ ​-​ ​F​R​O​N​T​ ​F​A​S​C​I​A​ ​W​/​ ​P​A​R​K​ ​A​S​S​I​S​T​ </stentry>
<stentry translate="no" props="part-number">1030348-00-A</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry translate="yes" props="comment">With holes for park assist sensors</stentry>
</strow>
<strow>
<stentry translate="no" props="annotation">2</stentry>
<stentry translate="no" props="part-name">B​r​a​c​k​e​t​ ​F​e​n​d​e​r​ ​F​R​T​ ​F​a​s​c​i​a​ ​L​e​f​t</stentry>
<stentry translate="no" props="part-number">6005888-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
...
</simpletable>


I'm trying to write an XSLT that compares the part numbers between the two XMLs. If they are equal, then write the part name from XML #1 to XML #2. So, the result would be:



<simpletable relcolwidth="5* 40* 15* 5* 35*">
<sthead translate="yes">
<stentry>No.</stentry>
<stentry>Part Description</stentry>
<stentry>Part Number</stentry>
<stentry>Qty</stentry>
<stentry>Comments</stentry>
</sthead>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="no" props="part-name">​Another test</stentry>
<stentry translate="no" props="part-number">1008590-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="no" props="part-name">Still another test</stentry>
<stentry translate="no" props="part-number">1030348-00-A</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry translate="yes" props="comment">With holes for park assist sensors</stentry>
</strow>
<strow>
<stentry translate="no" props="annotation">2</stentry>
<stentry translate="no" props="part-name">B​r​a​c​k​e​t​ ​F​e​n​d​e​r​ ​F​R​T​ ​F​a​s​c​i​a​ ​L​e​f​t</stentry>
<stentry translate="no" props="part-number">6005888-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
...
</simpletable>


The transform I have written so far is just not right and apparently oversimplified. The templates are not iterating the way that I would expect.


My XSLT:



<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns:xs="http://ift.tt/tphNwY"
exclude-result-prefixes="xs"
version="2.0">

<xsl:output method="xml" encoding="UTF-8" indent="yes" doctype-system="reference.dtd" doctype-public="-//OASIS//DTD DITA Reference//EN"/>

<xsl:param name="removeAttributesNamed" select="'|domains|ditaarch:DITAArchVersion|class|'"/>
<xsl:variable name="inputDoc" select="document('output-test.xml')/descendant::stentry[@props='part-number']"/>
<xsl:variable name="outputDoc" select="document('BODY.xml')/descendant::stentry[@props='part-number']"/>

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

<xsl:template match="@*">
<xsl:if test=
"not(contains($removeAttributesNamed,
concat('|', name(), '|')
)
)
">
<xsl:call-template name="ref"/>
</xsl:if>
</xsl:template>

<xsl:template name="combine">
<xsl:choose>
<xsl:when test="$inputDoc = $outputDoc">
<stentry translate="yes" props="part-name">
<xsl:for-each select="document('output-test.xml')/descendant::stentry[@props='part-name']">
<xsl:value-of select="text()"/>
</xsl:for-each>
</stentry>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="stentry[@props='part-name']">
<xsl:call-template name="combine"/>
</xsl:template>


With this XSLT, my result is instead:



<simpletable relcolwidth="5* 40* 15* 5* 35*">
<sthead translate="yes">
<stentry>No.</stentry>
<stentry>Part Description</stentry>
<stentry>Part Number</stentry>
<stentry>Qty</stentry>
<stentry>Comments</stentry>
</sthead>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="yes" props="part-name">This is a testAnother testStill another test</stentry>
<stentry translate="no" props="part-number">1008590-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
<strow>
<stentry translate="no" props="annotation">1</stentry>
<stentry translate="yes" props="part-name">This is a testAnother testStill another test</stentry>
<stentry translate="no" props="part-number">1030348-00-A</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry translate="yes" props="comment">With holes for park assist sensors</stentry>
</strow>
<strow>
<stentry translate="no" props="annotation">2</stentry>
<stentry translate="yes" props="part-name">This is a testAnother testStill another test</stentry>
<stentry translate="no" props="part-number">6005888-00-D</stentry>
<stentry translate="no" props="quantity">1</stentry>
<stentry/>
</strow>
...
</simpletable>


One of the problems that I see is it looks like I'm comparing node sequences as opposed to the actual string values of each part number. But I feel like I'm a bit in over my head right now and not sure how I would do that. So, I'm looking for some guidance that will help me refine my oh so rough solution.


No comments:

Post a Comment