2014-05-12 52 views
1

我有以下XML輸入使用XSLT

<Documents> 
    <Document> 
    <Prop> 
     <Name>FileNameField.BookingCentre</Name> 
     <Value>SG</Value> 
    </Prop> 
    <Prop> 
     <Name>FileNameField.MainType</Name> 
     <Value>CRE</Value> 
    </Prop> 
    <Prop> 
     <Name>FileNameField.SubType</Name> 
     <Value>CRR</Value> 
    </Prop> 
    <Prop> 
     <Name>FileNameField.AccountNo</Name> 
     <Value>8888888</Value> 
    </Prop> 
    <Prop> 
     <Name>FileNameField.Date</Name> 
     <Value>20140507</Value> 
    </Prop> 
    <Prop> 
     <Name>FileNameField.Time</Name> 
     <Value>183911</Value> 
    </Prop> 
    <Prop> 
     <Name>ServerIP</Name> 
     <Value>11.111.111.11</Value> 
    </Prop> 
    <Prop> 
     <Name>PhysicalLocation</Name> 
     <Value>AA</Value> 
    </Prop> 
    <Prop> 
     <Name>Destination Path</Name> 
     <Value>C:\Folder</Value> 
    </Prop> 
    <Prop> 
     <Name>File Name</Name> 
     <Value>SG_CRE_CRR_8888888_20140326172922.tif</Value> 
    </Prop> 
    <File>SG_CRE_CRR_8888888_20140326172922.tif</File> 
    </Document> 
</Documents> 

添加命名空間+前綴XML和我需要這成爲

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ad:AcceptDataInfo xmlns:ad="example.com"> 
<ad:ServerIP>11.111.111.11</ad:ServerIP> 
<ad:FilePath>\\11.111.111.11\Hardcoded folder\SG\CRE\CRR</ad:FilePath> 
<ad:FileName>SG_CRE_CRR_888888_20140326172922.tif</ad:FileName> 
<ad:XMLFilePath>\\11.111.111.11\Hardcoded folder\XML</ad:XMLFilePath> 
<ad:XMLFileName>SG_CRE_CRR_888888_20140326172922.XML</ad:XMLFileName> 
<ad:PhysicalLocation>AA</ad:PhysicalLocation> 
<ad:BookingCentre>SG</ad:BookingCentre> 
<ad:MainType>CRE</ad:MainType> 
<ad:SubType>CRR</ad:SubType> 
<ad:AccountNo>8888888</ad:AccountNo> 
<ad:Date>20140507</ad:Date> 
<ad:Time>183911</ad:Time> 
</ad:AcceptDataInfo> 

僅供參考,

輸出標籤包含的價值在ServerIP中,FileNameField.BookingCentre,FileNameField.MainType,FileNameField.SubType

out put標籤由ServerIP中的值組成,其他標籤只是硬編碼。

輸出標籤包含在文件名中的價值,但是擴展改爲.XML

輸入標籤:目標路徑和文件輸出XML被丟棄。

這裏是我的廢舊XSLT供參考,指出它還遠遠沒有完成。誰能幫我?謝謝。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ad="test"> 

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" standalone="yes" /> 
<xsl:strip-space elements="*"/> 

<!-- Copy Everything --> 
<xsl:template match="/"> 
<ad:AcceptDataInfo xmlns:ad="example.com"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</ad:AcceptDataInfo> 
</xsl:template> 

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

<xsl:template match= "Name[text()='FileNameField.Time']"> 
    <ad:Time> 
     <xsl:value-of select="FileNameField.Time"/> 
    </ad:Time> 
</xsl:template> 

<xsl:template match="Value[count(.|((//Value)[1])) = 1]"> 
    <index id="FilePath"> 
     <xsl:apply-templates /> 
    </index> 
</xsl:template> 

<xsl:template match="Name"/> 

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

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

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

</xsl:stylesheet> 

回答

1

試試下面的樣式表

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ad="example.com"> 

    <xsl:strip-space elements="*"/> 

    <xsl:output indent="yes"/> 

    <xsl:template match="/"> 
     <ad:AcceptDataInfo xmlns:ad="example.com"> 
      <ad:ServerIP> 
       <xsl:value-of select="Documents/Document/Prop[Name='ServerIP']/Value"/> 
      </ad:ServerIP> 
      <ad:FilePath> 
       <xsl:value-of select="concat('\\', Documents/Document/Prop[Name='ServerIP']/Value, '\Hardcoded folder\', Documents/Document/Prop[Name='FileNameField.BookingCentre']/Value, '\', Documents/Document/Prop[Name='FileNameField.MainType']/Value, '\', Documents/Document/Prop[Name='FileNameField.SubType']/Value)"/> 
      </ad:FilePath> 
      <ad:FileName> 
       <xsl:value-of select="Documents/Document/Prop[Name='File Name']/Value"/> 
      </ad:FileName> 
      <ad:XMLFilePath> 
       <xsl:value-of select="concat('\\', Documents/Document/Prop[Name='ServerIP']/Value, '\Hardcoded folder\XML')"/> 
      </ad:XMLFilePath> 
      <ad:XMLFileName> 
       <xsl:value-of select="concat(substring-before(Documents/Document/Prop[Name='File Name']/Value, '.'), '.XML')"/> 
      </ad:XMLFileName> 
      <ad:PhysicalLocation> 
       <xsl:value-of select="Documents/Document/Prop[Name='PhysicalLocation']/Value"/> 
      </ad:PhysicalLocation> 
      <ad:BookingCentre> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.BookingCentre']/Value"/> 
      </ad:BookingCentre> 
      <ad:MainType> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.MainType']/Value"/> 
      </ad:MainType> 
      <ad:SubType> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.SubType']/Value"/> 
      </ad:SubType> 
      <ad:AccountNo> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.AccountNo']/Value"/> 
      </ad:AccountNo> 
      <ad:Date> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.Date']/Value"/> 
      </ad:Date> 
      <ad:Time> 
       <xsl:value-of select="Documents/Document/Prop[Name='FileNameField.Time']/Value"/> 
      </ad:Time> 
     </ad:AcceptDataInfo> 
    </xsl:template> 

</xsl:stylesheet>