tags:

views:

25

answers:

2

Hi,

i got an xml-file with some elements. For some of these is an aqvivalent in a parameter xml-file along with some other elements. I want to add these other elements from parm-file as parameter to output file if element-names are matching. (the Attributes should only be generated if an element "InvoiceHeader" exists in the source-xml.

Here is my code...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"&gt;  
    <xsl:variable name="rpl" select="document('ParamInvoice.xml')"></xsl:variable>
       <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
       <xsl:template match="/">
             <xsl:apply-templates></xsl:apply-templates>
       </xsl:template>
       <xsl:template match="*">
          <xsl:copy>
          <xsl:if test="$rpl/StoraInvoice/local-name()">
            <xsl:call-template name="AttributeErzeugen">
               <xsl:with-param name="attr" select="$rpl/StoraInvoice/local-name()"></xsl:with-param>
      </xsl:call-template>
     </xsl:if>
    <xsl:apply-templates></xsl:apply-templates>
 </xsl:copy>
</xsl:template>
     <xsl:template name="AttributeErzeugen">
        <xsl:param name="attr"></xsl:param>
            <xsl:for-each select="$attr">
              <xsl:attribute name="{Attibute/@name}"><xsl:value-of select="."></xsl:value-  of></xsl:attribute>  
         </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>

and here the param-file

<?xml version="1.0" encoding="UTF-8"?>
<StoraInvoice>
   <InvoiceHeader>
      <Attribute name="Fuehrend">YYY</Attribute>
      <Attribute name="Feld">FFFF</Attribute>
      <Attribute name="Format">XYZXYZ</Attribute>
   </InvoiceHeader>
</StoraInvoice>

Siegfried

+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my"
 exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <my:rpl>
  <StoraInvoice>
   <t>
     <InvoiceHeader>
      <Attribute name="Fuehrend">YYY</Attribute>
      <Attribute name="Feld">FFFF</Attribute>
      <Attribute name="Format">XYZXYZ</Attribute>
     </InvoiceHeader>
   </t>
  </StoraInvoice>
 </my:rpl>


 <xsl:variable name="rpl" select="document('')/*/my:rpl"/>

 <xsl:template match="*">
   <xsl:variable name="vInvoiceElement" select=
       "$rpl/StoraInvoice/*[name()=name(current())]"/>

   <xsl:copy>
     <xsl:if test="$vInvoiceElement">
        <xsl:call-template name="AttributeErzeugen">
          <xsl:with-param name="pInvoiced" select="$vInvoiceElement"/>
        </xsl:call-template>
     </xsl:if>
     <xsl:apply-templates/>
   </xsl:copy>
 </xsl:template>

 <xsl:template name="AttributeErzeugen">
  <xsl:param name="pInvoiced"/>
  <xsl:for-each select="$pInvoiced/InvoiceHeader/Attribute">
    <xsl:attribute name="{@name}">
     <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t/>

produces the wanted, correct result:

<t Fuehrend="YYY" Feld="FFFF" Format="XYZXYZ"/>
Dimitre Novatchev
A: 

many thanks!!! That helps a lot...

siegfried storr