Question

I have the following XML input

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

And I need this to become

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

the output tag consist of the value in ServerIP, FileNameField.BookingCentre, FileNameField.MainType, FileNameField.SubType

the output tag consist of the value in ServerIP, others are hardcoded only.

the output tag consist of the value in File Name, but the extension changed to .xml

the input tag: Destination Path, and File are discarded in the output xml.

Here is my scrap XSLT for reference, noted that it is far from complete. Can anyone help me? thank you.

<?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>
Was it helpful?

Solution

try the following stylesheet

<?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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top