2010-04-29 50 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"/>