XSLT generating attributes if source-Element is in parameterfile

Posted by Siegfried on Stack Overflow See other posts from Stack Overflow or by Siegfried
Published on 2010-04-29T12:02:30Z Indexed on 2010/04/29 12:07 UTC
Read the original article Hit count: 329

Filed under:

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">  
    <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

© Stack Overflow or respective owner

Related posts about xslt