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


i apologize if seems obtuse or oversimplified. i'm no expert xslt.

i have 2 xmls:

xml #1

<simpletable>     <strow>      <stentry props="part-name">this 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 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 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 write xslt compares part numbers between 2 xmls. if equal, write part name xml #1 xml #2. so, result 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 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 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 have written far not right , apparently oversimplified. templates not iterating way expect.

my xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"  xmlns:xs="http://www.w3.org/2001/xmlschema"  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 xslt, result 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 testanother teststill 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 testanother teststill 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 park assist sensors</stentry>            </strow>            <strow>               <stentry translate="no" props="annotation">2</stentry>               <stentry translate="yes" props="part-name">this testanother teststill test</stentry>               <stentry translate="no" props="part-number">6005888-00-d</stentry>               <stentry translate="no" props="quantity">1</stentry>               <stentry/>            </strow>            ... </simpletable> 

one of problems see looks i'm comparing node sequences opposed actual string values of each part number. feel i'm bit in on head right , not sure how that. so, i'm looking guidance me refine oh rough solution.

the transform have written far not right , apparently oversimplified.

imho, over-complicating this, not over-simplifying.

assuming processing document marked xml #2, , supposing other document, marked xml #1, named "otherfile.xml", following dstylesheet:

xslt 2.0

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:key name="part" match="strow" use="stentry[@props='part-number']" />  <!-- identity transform --> <xsl:template match="@*|node()">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="stentry[@props='part-name']">     <xsl:copy>         <xsl:apply-templates select="@*"/>         <xsl:variable name="matching-part" select="key('part', ../stentry[@props='part-number'], document('otherfile.xml'))" />         <xsl:choose>             <xsl:when test="$matching-part">                 <xsl:value-of select="$matching-part/stentry[@props='part-name']"/>             </xsl:when>             <xsl:otherwise>                 <xsl:value-of select="."/>             </xsl:otherwise>         </xsl:choose>     </xsl:copy> </xsl:template>  </xsl:stylesheet> 

should return requested result. note use of key lookup values other file.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -