I am trying to split the file below to 3 items each and also keep the last section the same. Additionally, I also need to Header block that appears at the beginning with each split (except the last).
Input file
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root>
<List>
<Header>
<test1>a</test1>
</Header>
<Item>
<ItemNumber>
<Number>1</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>2</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>3</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>4</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>5</Number>
</ItemNumber>
</Item>
</List>
<List>
<EOF MaxMsgPerFile="3" >
</EOF>
</List>
</Root>
Here is what I have tried
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="tag">
<xsl:value-of select="/*/*/EOF/#MaxMsgPerFile"/>
</xsl:variable>
<xsl:template match="/Root">
<xsl:copy>
<xsl:for-each select="List[not (EOF)]/Item[position() mod $tag = 1]">
<List>
<xsl:copy-of select="Header"/>
<xsl:copy-of select=". | following-sibling::Item[position() < $tag]"/>
</List>
</xsl:for-each>
<xsl:copy-of select="List[EOF]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I get everything I wanted but I cant see the Header appearing at each split
Here is what is expected
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<List>
<Header>
<test1>a</test1>
</Header>
<Item>
<ItemNumber>
<Number>1</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>2</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>3</Number>
</ItemNumber>
</Item>
</List>
<List>
<Header>
<test1>a</test1>
</Header>
<Item>
<ItemNumber>
<Number>4</Number>
</ItemNumber>
</Item>
<Item>
<ItemNumber>
<Number>5</Number>
</ItemNumber>
</Item>
</List>
<List>
<EOF MaxMsgPerFile="3">
</EOF>
</List>
</Root>
Any help is greatly appreciated
Thanks
How about:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Root">
<xsl:variable name="groupSize" select="List/EOF/#MaxMsgPerFile" />
<xsl:copy>
<xsl:for-each select="List/Item[position() mod $groupSize = 1]">
<List>
<xsl:copy-of select="../Header | . | following-sibling::Item[position() < $groupSize]"/>
</List>
</xsl:for-each>
<xsl:copy-of select="List[EOF]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Related
Please note that source xml fieldscome in sequence means one student's info then another student info.
Can you help on the XSLT to break xml as one group node for each student
My Input is Below
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<item>
<columnName>StudentID</columnName>
<value>2356</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Java</value>
</item>
<item>
<columnName>City</columnName>
<value>Chicago</value>
</item>
<item>
<columnName>StudentID</columnName>
<value>4578</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Net</value>
</item>
<item>
<columnName>City</columnName>
<value>NewYork</value>
</item>
<item>
<columnName>StudentID</columnName>
<value>5892</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Office</value>
</item>
<item>
<columnName>City</columnName>
<value>Dallas</value>
</item>
</root>
I need Output
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<Record>
<item>
<columnName>StudentID</columnName>
<value>2356</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Java</value>
</item>
<item>
<columnName>City</columnName>
<value>Chicago</value>
</item>
</Record>
<Record>
<item>
<columnName>StudentID</columnName>
<value>4578</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Net</value>
</item>
<item>
<columnName>City</columnName>
<value>NewYork</value>
</item>
</Record>
<Record>
<item>
<columnName>StudentID</columnName>
<value>5892</value>
</item>
<item>
<columnName>StudentName</columnName>
<value>Office</value>
</item>
<item>
<columnName>City</columnName>
<value>Dallas</value>
</item>
</Record>
</root>
Please note that source xml fileds come in sequence means one student info then another student info.
Can you help on the XSLT to break xml as one group node for each student
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="root">
<root>
<Record>
<xsl:apply-templates select="data"/>
</Record>
<Record/>
</root>
</xsl:template>
<xsl:template match="data">
<data>
<xsl:apply-templates select="columnName"/>
<xsl:apply-templates select="value"/>
</data>
</xsl:template>
<xsl:template match="columnName">
<columnName>
<xsl:value-of select="."/>
</columnName>
</xsl:template>
<xsl:template match="value">
<value>
<xsl:value-of select="."/>
</value>
</xsl:template>
</xsl:stylesheet>
Please note that source xml fileds come in sequence means one student info then another student info.
Can you help on the XSLT to break xml as one group node for each student
Assuming that items always come in groups of three, you could do simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="item[position() mod 3 = 1]">
<Record>
<xsl:copy-of select=". | following-sibling::item[position() < 3]"/>
</Record>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Need to write an xslt file. Below is the input file:
<assets>
<item>
<child1>some text</child1>
<child2>some text</child2>
<child3>some text</child3>
<child4>some text</child4>
</item>
<item>
<child1>some text</child1>
<child2>some text</child2>
<childx>some text</childx>
</item>
<item>
<child1>some text</child1>
<childx>some text</childx>
<childy>some text</childy>
<childz>some text</childz>
</item>
</assets>
I need to find out all the unique child names of assets/item. The number of children and child name is dynamic under the element (item)
The Output should be as below:
<item>
<columns>
<columnname>child1</columnname>
<columnname>child2</columnname>
<columnname>child3</columnname>
<columnname>child4</columnname>
<columnname>childx</columnname>
<columnname>childy</columnname>
<columnname>childz</columnname>
</columns>
</item>
You can use Muenchian grouping on the element names - something like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="elements" match="*" use="local-name()" />
<xsl:template match="/">
<item>
<xsl:apply-templates select="assets/item" />
</item>
</xsl:template>
<xsl:template match="item">
<xsl:for-each select="*[count(.|key('elements', local-name())[1]) = 1]">
<columnname>
<xsl:value-of select="name()"/>
</columnname>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Will get you this:
<item>
<columnname>child1</columnname>
<columnname>child2</columnname>
<columnname>child3</columnname>
<columnname>child4</columnname>
<columnname>childx</columnname>
<columnname>childy</columnname>
<columnname>childz</columnname>
</item>
you can use this
<xsl:key name="child" match="item/*" use="name()"/>
<xsl:template match="/">
<item>
<columns>
<xsl:for-each select="//item/*[count(.|key('child', name())[1]) = 1]">
<columnname><xsl:value-of select="name()"/></columnname>
</xsl:for-each>
</columns>
</item>
</xsl:template>
output
<item>
<columns>
<columnname>child1</columnname>
<columnname>child2</columnname>
<columnname>child3</columnname>
<columnname>child4</columnname>
<columnname>childx</columnname>
<columnname>childy</columnname>
<columnname>childz</columnname>
</columns>
</item>
I am trying to write a XSLT which extracts items matches the conditions listed in another file.
INPUT FILE (input.xml)
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>DEF</Product>
<Price>20.00</Price>
</Item>
<Item>
<Product>GHI</Product>
<Price>30.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
External List File (Codes.xml)
<ProductCodeList>
<ProductCode>ABC</ProductCode>
<ProductCode>JKL</ProductCode>
</ProductCodeList>
Expected Output (output.xml)
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
Could you please show me which one is not working?
<xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />`
<xsl:template match="/">
<xsl:apply-templates select="/ItemList/Item[Product=$productCodeList]"/>
</xsl:template>
<xsl:template match="/ItemList/Item">
<xsl:copy-of select="."/>
</xsl:template>
This simple (no conditionals, no current()) transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vProds" select=
"document('file:///c:/temp/delete/ProductList.xml')"/>
<xsl:template match="/">
<ItemList>
<xsl:copy-of select=
"/*/Item
[Product
=
$vProds/*/ProductCode
]
"/>
</ItemList>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>DEF</Product>
<Price>20.00</Price>
</Item>
<Item>
<Product>GHI</Product>
<Price>30.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
and having the provided Productlist.xml stored at c:\temp\delete:
<ProductCodeList>
<ProductCode>ABC</ProductCode>
<ProductCode>JKL</ProductCode>
</ProductCodeList>
produces the wanted, correct result:
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
Does that maybe work better?
<xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />
<xsl:template match="/">
<xsl:apply-templates select="/ItemList/Item"/>
</xsl:template>
<xsl:template match="/ItemList/Item">
<xsl:if test="$productCodeList[.=current()/Product]">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
How to do splitbyvalue function using XSLT?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<ACCOUNT>
<xsl:apply-templates select="//RefCoded/RefCode[. = 'WBS']"/>
</ACCOUNT>
</xsl:template>
<xsl:template match="RefCode">
<item>
<BItemNum>
<xsl:value-of select="../../../../LineItemNum/BLineItemNum"/>
</BItemNum>
</item>
</xsl:template>
</xsl:stylesheet>
Output:
<ACCOUNT>
<item>
<BItemNum>00001</BItemNum>
</item>
<item>
<BItemNum>00001</BItemNum>
</item>
<item>
<BItemNum>00002</BItemNum>
</item>
<item>
<BItemNum>00002</BItemNum>
</item>
</ACCOUNT>
<xsl:template match="/">
<ACCOUNT>
<xsl:for-each select="descendant::RefCode[text() = 'WBS']">
<item>
<BItemNum><xsl:value-of select="ancestor::ItemDetail/descendant::BLineItemNum"/></BItemNum>
</item>
</xsl:for-each>
</ACCOUNT>
</xsl:template>
I need to split the following XML file based on a predetermined value, for this example, lets assume I want to limit the "Item" node to three (3) within each file created.
Here's a sample input XML file:
<Items>
<Item>
<Title>Title 1</Title>
<DueDate>01-02-2008</DueDate>
</Item>
<Item>
<Title>Title 2</Title>
<DueDate>01-02-2009</DueDate>
</Item>
<Item>
<Title>Title 3</Title>
<DueDate>01-02-2010</DueDate>
</Item>
<Item>
<Title>Title 4</Title>
<DueDate>01-02-2011</DueDate>
</Item>
<Item>
<Title>Title 5</Title>
<DueDate>01-02-2012</DueDate>
</Item>
<Item>
<Title>Title 6</Title>
<DueDate>01-02-2013</DueDate>
</Item>
<Item>
<Title>Title 7</Title>
<DueDate>01-02-2013</DueDate>
</Item>
</Items>
The desired output based on the threshold value of 3, would be three files, two of which contain 3 "Item", and the last one containing the remaining "items", which would be one.
Here's a sample of my XSLT which does allow me to split them for each item, which results into seven separate files, however, what I desire is to limit the size of the file based on a certain limit of "Item" node per file.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes" name="xml" />
<xsl:template match="/">
<xsl:for-each select="//Item">
<xsl:variable name="nTitle" select="Title"/>
<xsl:variable name="filename" select="concat('Items\',$nTitle,'-','.xml')" />
<xsl:value-of select="$filename" />
<xsl:result-document href="{$filename}" format="xml">
<xsl:copy-of select="."/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pItemsNumber" select="3"/>
<xsl:template match="Items">
<xsl:for-each-group select="Item"
group-adjacent="(position()-1) idiv $pItemsNumber">
<xsl:result-document href="Items\{current-grouping-key()}.xml">
<Items>
<xsl:copy-of select="current-group()"/>
</Items>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Title>Title 1</Title>
<DueDate>01-02-2008</DueDate>
</Item>
<Item>
<Title>Title 2</Title>
<DueDate>01-02-2009</DueDate>
</Item>
<Item>
<Title>Title 3</Title>
<DueDate>01-02-2010</DueDate>
</Item>
</Items>
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Title>Title 4</Title>
<DueDate>01-02-2011</DueDate>
</Item>
<Item>
<Title>Title 5</Title>
<DueDate>01-02-2012</DueDate>
</Item>
<Item>
<Title>Title 6</Title>
<DueDate>01-02-2013</DueDate>
</Item>
</Items>
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Title>Title 7</Title>
<DueDate>01-02-2013</DueDate>
</Item>
</Items>
Edit: Oops!
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pSplitNum" select="3"/>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item[position() mod $pSplitNum eq 1]">
<xsl:result-document href=
"file{position()}-{min((position()+$pSplitNum -1, count(/*/Item)))}.xml">
<Items>
<xsl:call-template name="identity"/>
<xsl:apply-templates mode="copy" select=
"following-sibling::Item[position() lt $pSplitNum]"/>
</Items>
</xsl:result-document>
</xsl:template>
<xsl:template match="/*"><xsl:apply-templates/></xsl:template>
<xsl:template match="Item[position() mod $pSplitNum ne 1]"/>
</xsl:stylesheet>
when applied on the provided XML document:
<Items>
<Item>
<Title>Title 1</Title>
<DueDate>01-02-2008</DueDate>
</Item>
<Item>
<Title>Title 2</Title>
<DueDate>01-02-2009</DueDate>
</Item>
<Item>
<Title>Title 3</Title>
<DueDate>01-02-2010</DueDate>
</Item>
<Item>
<Title>Title 4</Title>
<DueDate>01-02-2011</DueDate>
</Item>
<Item>
<Title>Title 5</Title>
<DueDate>01-02-2012</DueDate>
</Item>
<Item>
<Title>Title 6</Title>
<DueDate>01-02-2013</DueDate>
</Item>
<Item>
<Title>Title 7</Title>
<DueDate>01-02-2013</DueDate>
</Item>
</Items>
produces the wanted three xml files:
Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_22
Stylesheet compilation time: 645 milliseconds
Processing file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
Building tree for file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml using class net.sf.saxon.tinytree.TinyBuilder
Tree built in 10 milliseconds
Tree size: 38 nodes, 119 characters, 0 attributes
Loading net.sf.saxon.event.MessageEmitter
Writing to file:/C:/Program%20Files/Java/jre6/bin/file1-3.xml
Writing to file:/C:/Program%20Files/Java/jre6/bin/file4-6.xml
Writing to file:/C:/Program%20Files/Java/jre6/bin/file7-7.xml
Execution time: 101 milliseconds
Memory used: 11453088
NamePool contents: 20 entries in 20 chains. 6 prefixes, 7 URIs
Do note:
This is a simple application of the identity rule pattern.
Every Item starting a new file is matched and it causes the wrapping in a top element, processing of itself and the next $pSplitNum -1 (or whatever remains in the last group), and outputting this as a single result-document (file).
The name of every file created is: "filex-y.xml", where x and y are the starting and ending indexes of the Item elements written in the file.
Every Item that isn't starting a new file is deleted by an empty matching template. Such elements are processed in "copy" mode.
You could implement a counter that is declared outside of your loop. When the counter hits 3, reset it and set a new filename. Otherwise, increment and append to the existing filename.