xsl-list
[Top] [All Lists]

Re: can you select name() of attributes?

2004-07-27 04:01:16
Hi again David (and others),

The input and some of my problem should be in my earlier post "Matching attributes in two documents", but I have tried rewriting it a little to match this particular problem.

If some or all of this is not understandable, please tell me, and I will try to elaborate.


Doc1.xml
<Input>
 <I q="input value" s="Invalid attribute">Input</I>
</Input>

Doc2.xml
<A>
 <B q="default" r="default">Text1</B>
</A>

Desired output:
<Output q="input value" r="default">Some text in the output element</Output>

Output that I get:
<Output q="default" r="default">Some text in the output element</Output>

If the attribute exists in both Doc1 and Doc2, then I want the value of Doc1 (input value). If it exists only in Doc1, then it should be ignored (invalid attribute), and if only in Doc2, then I want the value of Doc2 (default)

XSL snippets (for the non-desired output):
<!-- Global variables -->
<xsl:variable name="Doc1" select="document('Doc1.xml')/Input/I"/>
<xsl:variable name="Doc2" select="document('Doc2.xml')/A/B"/>


<!-- Template for processing -->
<xsl:template name="MakeOutput">
<xsl:variable name="Attr1" select="$Doc1/@*"/>
<xsl:variable name="Attr2" select="$Doc2/@*"/>

<Output>
<xsl:for-each select="$Attr2">
 <xsl:variable name="Attr" select="."/>
   <xsl:if test="not($Attr1)">
<xsl:attribute name="{name($Attr)}"><xsl:value-of select="$Attr"/></xsl:attribute>
   </xsl:if>
 <xsl:for-each select="$Attr1[name()=name($Attr)]">
   <xsl:choose>
     <xsl:when test="name()=name($Attr)">
<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
     </xsl:when>
     <xsl:otherwise>
<xsl:attribute name="{name($Attr)}"><xsl:value-of select="$Attr"/></xsl:attribute>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:for-each>
</xsl:for-each>
Some text in the output element
</Output>
</xsl:template>

Regards,
Ragulf Pickaxe :-)

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail