2017-08-30 81 views
0

我有這個源XML:插入部分插入XML文檔

<?xml version="1.0" encoding="UTF-8"?> 
<SyncSupplierInvoice xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.8.0"> 
    <UserArea> 
     <Property> 
      <NameValue name="TypeCode">PS</NameValue> 
     </Property> 
     <Property> 
      <NameValue name="TaxCode">TGPLG180</NameValue> 
     </Property> 
     <Property> 
      <NameValue name="TaxOrg">*</NameValue> 
     </Property> 
    </UserArea> 
</SyncSupplierInvoice> 

3在UserArea物業部分。我想插入第4部分,customTaxCode,它包含TaxCode的最後一個字符。像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<SyncSupplierInvoice xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.8.0"> 
    <UserArea> 
     <Property> 
      <NameValue name="TypeCode">PS</NameValue> 
     </Property> 
     <Property> 
      <NameValue name="TaxCode">TGPLG180</NameValue> 
     </Property> 
     <Property> 
      <NameValue name="customTaxCode">0</NameValue> 
     </Property> 
     <Property> 
      <NameValue name="TaxOrg">*</NameValue> 
     </Property> 
    </UserArea> 
</SyncSupplierInvoice> 

我的XSLT只能部分工作。問題在於它在現有的Property元素下創建Property元素,而不是作爲兄弟。我不知道如何實現結果。提前感謝您的任何建議。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="//my:UserArea/my:Property/my:NameValue[@name='TaxCode']"> 
     <xsl:copy-of select="."/> 
    <Property> 
     <NameValue name="customTaxCode"> 
      <xsl:value-of select="substring(., string-length(.), 1)" /> 
     </NameValue> 
    </Property> 
    </xsl:template> 
</xsl:stylesheet> 

回答

0

而不是你的模板匹配my:NameValue的,改變它相匹配的相關my:Property代替。

<xsl:template match="//my:UserArea/my:Property[my:NameValue/@name='TaxCode']"> 

或者更好的是,這一點,作爲起始模板匹配//是不必要的。

<xsl:template match="my:Property[my:NameValue/@name='TaxCode']"> 

請注意,您還應該添加一個默認的命名空間聲明,您的XSLT,以確保在創建新Property元素是相同的命名空間的源

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
          xmlns:my="http://schema.infor.com/InforOAGIS/2" 
          xmlns="http://schema.infor.com/InforOAGIS/2" 
          xmlns:java="http://xml.apache.org/xslt/java" 
          exclude-result-prefixes="my java"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="my:Property[my:NameValue/@name='TaxCode']"> 
     <xsl:copy-of select="."/> 
    <Property> 
     <NameValue name="customTaxCode"> 
      <xsl:value-of select="substring(my:NameValue, string-length(my:NameValue), 1)" /> 
     </NameValue> 
    </Property> 
    </xsl:template> 
</xsl:stylesheet> 
0

使用這個

<xsl:template match="//my:UserArea/my:Property[my:NameValue[@name='TaxCode']]"> 

問題發生在y ou使用它作爲NameValue lavel