【问题标题】:add namespace + prefix to XML using XSLT使用 XSLT 向 XML 添加命名空间 + 前缀
【发布时间】:2014-05-12 06:32:59
【问题描述】:

我有以下 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>

仅供参考,

输出标签由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>

【问题讨论】:

    标签: xml xslt


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 2012-10-22
      相关资源
      最近更新 更多