2010-04-29 51 views
0

我得到了一些带有一些元素的xml文件。对于其中的一些,它是一个参数xml文件中的一个有效值以及其他一些元素。 如果元素名称匹配,我想从parm-file中将这些其他元素作为参数添加到输出文件。 (属性应如果一个元素“InvoiceHeader”源代码中的XML只存在产生。如果source-Element在参数文件中,XSLT生成属性

这里是我的代码...

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

这里帕拉姆文件

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

齐格弗里德

+0

很好的问题(+1)。查看我的答案以获得解决方案。 :) – 2010-04-29 13:29:23

回答

1

这种转变

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

当这个XML文档施加:

<t/> 

产生想要的,正确的结果

<t Fuehrend="YYY" Feld="FFFF" Format="XYZXYZ"/>