Solved

Breaking XML into different nodes/sections

Posted on 2013-06-25
4
261 Views
Last Modified: 2013-06-26
Experts,

I have a XML and XSL I'm trying to format.  Currently its combining my "Detail" nodes together, so everywhere  it sees "Detail" it adds.  This is fine, but I want every detail I want it to spawn a new XML collection with the related tags under it like in target.xml I attached.  Any idea how I can have it break every detail section into its own collection while keeping header information in there?

Below is my XSLT:

<!-- Begin XST Style Sheet -->


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" >

<!-- Indent in the output -->
<xsl:output indent="yes"/>

<!-- Match the root -->
<xsl:template match="ProcessImage">
<Import> <!-- Insert our Import Node -->
<Archive ConnectionID="1" Name="1"> <!-- Define our Application Connection String -->
<Document pass="True"> <!-- Statically set documents to pass -->
<xsl:for-each select="Pages/Page"> <!-- Ignore -->
<DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
</xsl:for-each>
<Fields> <!-- Grab the field data and house it under the Fields Node -->
<xsl:for-each select="DataArea/Image/Header/*[not(self::Count)]"> <!-- Ignore Count if Present -->
<Field Name="{name(.)}" value="{./text()}" pass="True"/>
</xsl:for-each>
<xsl:for-each select="DataArea/Image/Detail/*[not(self::ImageData)]"> <!-- Ignore Image Data if Present -->
<Field Name="{name(.)}" value="{./text()}" pass="True"/>
</xsl:for-each>
</Fields>
</Document>
</Archive>
</Import>
</xsl:template>


</xsl:stylesheet>

Open in new window

target.xml
sample.xml
whatitsdoingnow.xml
0
Comment
Question by:lm1189
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39275352
Something like this?

Note that it is now that breaking up the templates as I showed you before really helps keeping teh code readable

<!-- Begin XST Style Sheet -->


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" >
  
  <!-- Indent in the output -->
  <xsl:output indent="yes"/>
  
  <!-- Match the root -->
  <xsl:template match="ProcessImage">
    <Import> <!-- Insert our Import Node -->
      <Archive ConnectionID="1" Name="1"> <!-- Define our Application Connection String -->
        <Document pass="True"> <!-- Statically set documents to pass -->
          <xsl:for-each select="Pages/Page"> <!-- Ignore -->
            <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
          </xsl:for-each>
          <xsl:apply-templates select="DataArea/Image/Header | DataArea/Image/Detail"/>
        </Document>
      </Archive>
    </Import>
  </xsl:template>
  
  <xsl:template match="Header">
    <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
    <Fields>
      <xsl:apply-templates select="*[not(self::Count)]"/> <!-- Ignore Count if Present -->
    </Fields>
  </xsl:template>
  
  <xsl:template match="Detail">
    <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
    <Fields>
      <xsl:apply-templates select="*[not(self::ImageData)]"/> <!-- Ignore Image Data if Present -->
    </Fields>
  </xsl:template>
  
  <xsl:template match="Header/* | Detail/*">
    <Field Name="{name()}" value="{.}" pass="True"/>
  </xsl:template>
  
  
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39275372
However, it seems you want the Header repeated

<!-- Begin XST Style Sheet -->


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" >
  
  <!-- Indent in the output -->
  <xsl:output indent="yes"/>
  
  <!-- Match the root -->
  <xsl:template match="ProcessImage">
    <Import> <!-- Insert our Import Node -->
      <Archive ConnectionID="1" Name="1"> <!-- Define our Application Connection String -->
        <Document pass="True"> <!-- Statically set documents to pass -->
          <xsl:for-each select="Pages/Page"> <!-- Ignore -->
            <DocFile FileLoc="{@FileName}" /> <!-- Ignore -->
          </xsl:for-each>
          <xsl:apply-templates select="DataArea/Image/Detail"/>
        </Document>
      </Archive>
    </Import>
  </xsl:template>
  
  <xsl:template match="Detail">
    <DocFile FileLoc="C:\SHARE FOLDER\SCANNED FILES\Scan{format-number(position() - 1, '0000')}.tif" /> <!-- Ignore -->
    <Fields>
      <xsl:apply-templates select="parent::Image/Header/*[not(self::Count)]"/> <!-- Ignore Count if Present -->
      <xsl:apply-templates select="*[not(self::ImageData)]"/> <!-- Ignore Image Data if Present -->
    </Fields>
  </xsl:template>
  
  <xsl:template match="Header/* | Detail/*">
    <Field Name="{name()}" value="{.}" pass="True"/>
  </xsl:template>
  
  
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:lm1189
ID: 39275721
Thank you!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39277210
welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 116
VB.Net and XML XSD Files 1 53
Trouble parsing soap xml result 3 40
Transform XML to Excel using XSL 1 23
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now