使用XSLT将名称空间+前缀添加到XML

时间:2014-05-12 06:33:00

标签: xml xslt

我有以下XML输入

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

FYI,

输出标记由ServerIP,FileNameField.BookingCentre,FileNameField.MainType,FileNameField.SubType

中的值组成

输出标记由ServerIP中的值组成,其他标记仅由硬编码。

输出标记由文件名中的值组成,但扩展名更改为.xml

输入标记:Destination path和File在输出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 个答案:

答案 0 :(得分: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>