2017-08-30 109 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