I've seen many posts on this, but none of them have helped me figure out my problem.
Test1.xml
<table>
<row>
<col1>A</col1>
</row>
<row>
<col1>B</col1>
</row>
<row>
<col1>C</col1>
</row>
</table>
Test2.xml
<table>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>B</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>C</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>DEF</col2>
</row>
</table>
Test.xsl (XSLT 1.0)
<xsl:variable name="input" select="document('test1.xml')/>
<xsl:template match="/">
<xsl:apply-templates select="$input" mode="special"/>
</xsl:template>
<xsl:template match="node()|#" mode="special">
<xsl:copy>
<xsl:apply-templates select="node()|#" mode="special"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Row" mode="special">
<xsl:variable name="cols" select="document('test2.xml')/Table/Row[current()/Col1 = Col1]/Col2"/>
<xsl:variable name="unique_cols" select="$cols[not(. = preceding-sibling::*)]"/>
<!-- Debug -->
<xsl:for-each select="$unique_cols">
<xsl:copy-of select="."/>
</xsl:for-each>
----
</xsl:template>
Expected Output:
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
Current Output:
<col2>ABC</col2>
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
The col2 values in $unique_cols should be distinct per col1 values. If unique col2 values could be selected in $cols, even better.
Just replace:
<xsl:variable name="unique_cols" select="$cols[not(. = preceding-sibling::*)]"/>
with:
<xsl:variable name="unique_cols" select=
"$cols[not(../col1 = ../preceding-sibling::*/col1)]"/>
The full transformation (initially corrrected to fix a multitude of lexical errors) now becomes (also using my own file-Uris):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="input" select=
"document('file:///c:/temp/delete/test1.xml')"/>
<xsl:template match="/">
<xsl:apply-templates select="$input" mode="special"/>
</xsl:template>
<xsl:template match="node()|#*" mode="special">
<xsl:copy>
<xsl:apply-templates select="node()|#*" mode="special"/>
</xsl:copy>
</xsl:template>
<xsl:template match="row" mode="special">
<xsl:variable name="cols" select=
"document('file:///c:/temp/delete/test2.xml')
/table
/row[current()/col1 = col1]
/col2"/>
<xsl:variable name="unique_cols" select=
"$cols[not(../col1 = ../preceding-sibling::*/col1)]"/>
<!-- Debug -->
<xsl:for-each select="$unique_cols">
<xsl:copy-of select="."/>
</xsl:for-each>
----
</xsl:template>
</xsl:stylesheet>
and the files are:
c:/temp/delete/test1.xml:
<table>
<row>
<col1>A</col1>
</row>
<row>
<col1>B</col1>
</row>
<row>
<col1>C</col1>
</row>
</table>
and: c:/temp/delete/test2.xml:
<table>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>B</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>C</col1>
<col2>ABC</col2>
</row>
</table>
When the transformation is performed on any XML document (not used), the wanted, correct result is produced:
<table>
<col2>ABC</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
----
</table>
I think I may have figured this out based on an answer provided on another post (https://groups.google.com/forum/?fromgroups#!topic/microsoft.public.xsl/i8FwJUD0r8U).
<xsl:variable name="cols" select=
"document('test2.xml')/table/row[current()/col1 = col1]/col2[not(. = ../preceding-sibling::*/col2[current()/col1 = ../col1])]"/>
This appears to select unique col2 values into $cols eliminating the need for the second variable.
Related
I have done few XSLT in the past, but I am facing challenge in this.
I am working with PLC tag, for each tag i am getting three rowset node, so after every three Rowset i need to create new "Row" group.
Updated with XSLT
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<Rowsets >
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<WC_ID>0001</WC_ID>
</Row>
</Rowset>
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<Tag1_Good>6817</Tag1_Good>
</Row>
</Rowset>
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<Tag1_Bad>0</Tag1_Bad>
</Row>
</Rowset>
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<WC_ID>0002</WC_ID>
</Row>
</Rowset>
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<Tag2_Good>6800</Tag2_Good>
</Row>
</Rowset>
<Rowset>
<Row>
<DateTime>2021-07-05T07:33:38</DateTime>
<Tag2_Bad>0</Tag2_Bad>
</Row>
</Rowset>
</Rowsets>
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<Rowset>
<Row>
<WC_ID>0001</WC_ID>
<Tag1_Good>6817</Tag1_Good>
<Tag1_Bad>0</Tag1_Bad>
</Row>
<Row>
<WC_ID>0002</WC_ID>
<Tag1_Good>6800</Tag1_Good>
<Tag1_Bad>0</Tag1_Bad>
</Row>
</Rowset>
My XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Rowsets >
<xsl:variable name="batchSize" select="3"/>
<Rowset>
<xsl:for-each select="/Rowsets/Rowset[position() mod $batchSize >= 0]"
<Row>
<xsl:value-of select="Row/*[2]" />
</Row>
</xsl:for-each>
</Rowset>
</Rowsets>
</xsl:template>
</xsl:stylesheet>
I am not able to make this into a new group
Use this:
<Rowsets>
<xsl:variable name="batchSize" select="3"/>
<Rowset>
<xsl:for-each select="/Rowsets/Rowset">
<xsl:variable name="pos" select="position()"/>
<xsl:if test="$pos mod $batchSize = 0">
<Row>
<WC_ID>
<xsl:value-of select="/Rowsets/Rowset[$pos - 2]/Row/*[2]"/>
</WC_ID>
<Tag1_Good>
<xsl:value-of select="/Rowsets/Rowset[$pos - 1]/Row/*[2]"/>
</Tag1_Good>
<Tag1_Bad>
<xsl:value-of select="/Rowsets/Rowset[$pos]/Row/*[2]"/>
</Tag1_Bad>
</Row>
</xsl:if>
</xsl:for-each>
</Rowset>
</Rowsets>
After reading a lot about this question already, I still do not find final solution for my problem as I am an absolut beginner with xsl.
I want to add all attributes of child nodes to parent level.
This is what I have:
<rankings date="2021-03-15">
<ranking rank="1" rank_change="0" points="12008">
<player initials="" nationality="SRB" last_name="Djokovic" first_name="Novak" id="7" display_name="Novak Djokovic"/>
</ranking>
<ranking rank="2" rank_change="1" points="9940">
<player initials="" nationality="RUS" last_name="Medvedev" first_name="Daniil" id="35844" display_name="Daniil Medvedev"/>
</ranking>
<ranking rank="3" rank_change="-1" points="9670">
<player initials="" nationality="ESP" last_name="Nadal" first_name="Rafael" id="4" display_name="Rafael Nadal"/>
</ranking>
</rankings>
This is what I tried (miss identity tranform I think)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="rankings">
<data>
<xsl:apply-templates select="*"/>
</data>
</xsl:template>
<xsl:template match="ranking | player">
<row>
<xsl:apply-templates select="#* | node()"/>
</row>
</xsl:template>
<xsl:template match="ranking/#* | player/#*">
<xsl:element name="{name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
With following result:
<data>
<row>
<rank>1</rank>
<rank_change>0</rank_change>
<points>12008</points>
<row>
<initials/>
<nationality>SRB</nationality>
<last_name>Djokovic</last_name>
<first_name>Novak</first_name>
<id>7</id>
<display_name>Novak Djokovic</display_name>
</row>
</row>
</data>
This is my goal:
<data>
<row>
<rank>1</rank>
<rank_change>0</rank_change>
<points>12008</points>
<initials/>
<nationality>SRB</nationality>
<last_name>Djokovic</last_name>
<first_name>Novak</first_name>
<id>7</id>
<display_name>Novak Djokovic</display_name>
</row>
</data>
I hope one of you can help me with this.
Cheers,
Phil
try splitting ranking and player in its own template
<xsl:template match="ranking">
<row>
<xsl:apply-templates select="#* | node()"/>
</row>
</xsl:template>
<xsl:template match="player">
<xsl:apply-templates select="#* | node()"/>
</xsl:template>
Result:
<data>
<row>
<rank>1</rank>
<rank_change>0</rank_change>
<points>12008</points>
<initials/>
<nationality>SRB</nationality>
<last_name>Djokovic</last_name>
<first_name>Novak</first_name>
<id>7</id>
<display_name>Novak Djokovic</display_name>
</row>
<row>
<rank>2</rank>
<rank_change>1</rank_change>
<points>9940</points>
<initials/>
<nationality>RUS</nationality>
<last_name>Medvedev</last_name>
<first_name>Daniil</first_name>
<id>35844</id>
<display_name>Daniil Medvedev</display_name>
</row>
<row>
<rank>3</rank>
<rank_change>-1</rank_change>
<points>9670</points>
<initials/>
<nationality>ESP</nationality>
<last_name>Nadal</last_name>
<first_name>Rafael</first_name>
<id>4</id>
<display_name>Rafael Nadal</display_name>
</row>
</data>
If I am guessing correctly what your real goal is, 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="rankings">
<data>
<xsl:for-each select="ranking">
<row>
<xsl:for-each select=".//#*">
<xsl:element name="{name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</row>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
I need help with my XSLT. Here's my XML structure.
<root>
<row>
<component>mainfield_1</component>
<type>Field</type>
<where_used>
<component>subfield_2</component>
<type>Field</type>
</where_used>
<where_used>
<component>report_1</component>
<type>Report</type>
</where_used>
</row>
<row>
<component>subfield_2</component>
<type>Field</type>
<where_used>
<component>report_2</component>
<type>report</type>
</where_used>
</row>
<row>
<component>mainfield_3</component>
<type>Field</type>
</row>
</root>
I would like it to be transformed into the following:
<root>
<row>
<component>mainfield_1</component>
<type>Field</type>
</row>
<row>
<component>subfield_2</component>
<type>Field</type>
</row>
<row>
<component>report_1</component>
<type>Report</type>
</row>
<row>
<component>report_2</component>
<type>report</type>
</row>
</root>
Basically, I am trying to get all the distinct dependencies of component mainfield_1. Here's my sample code but it is not enough to find any matching parent that has the same component name as the children.
<xsl:template match="root">
<root>
<xsl:apply-templates select="row[component='mainfield_1']"/>
</root>
</xsl:template>
<xsl:template match="row">
<row>
<component>
<xsl:value-of select="component"/>
</component>
<type>
<xsl:value-of select="type" />
</type>
</row>
<xsl:apply-templates select="where_used"/>
</xsl:template>
<xsl:template match="where_used">
<row>
<component>
<xsl:value-of select="component"/>
</component>
<type>
<xsl:value-of select="type" />
</type>
</row>
</xsl:template>
If I run the above, I will not be able to get this.
<row>
<component>report_2</component>
<type>report</type>
</row>
Please help.
Consider using a key to look up the row items by component
<xsl:key name="rows" match="row" use="component" />
Then you can have a template for where_used nodes that refer to a separate row, allowing you to select that row instead
<xsl:template match="where_used[key('rows', component)]">
<xsl:apply-templates select="key('rows', component)" />
</xsl:template>
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="rows" match="row" use="component" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<root>
<xsl:apply-templates select="row[component='mainfield_1']"/>
</root>
</xsl:template>
<xsl:template match="row">
<row>
<xsl:apply-templates select="* except where_used" />
</row>
<xsl:apply-templates select="where_used"/>
</xsl:template>
<xsl:template match="where_used">
<row>
<xsl:apply-templates />
</row>
</xsl:template>
<xsl:template match="where_used[key('rows', component)]">
<xsl:apply-templates select="key('rows', component)" />
</xsl:template>
</xsl:stylesheet>
Note I have used the identity template too, to avoid having to explicitly copy existing nodes that don't need to be changed.
How can I count intermediary elements? I think the solution to this is related to this question. Supposing I had something like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<row>
<cell>Title</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
<cell>example additional cell</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
<row>
<cell>Title</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
<row>
<cell>Title</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
<row>
<cell>Title</cell>
</row>
<row>
<cell padding='true'>Chapter</cell>
</row>
</root>
And, applying a transformation like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="/root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="row">
<xsl:choose>
<!--these are the indented ones-->
<xsl:when test="cell/#padding">
<xsl:number
value="count(preceding-sibling::row[child::cell[1][#padding]]) + 1"
format="a. "/>
</xsl:when>
<xsl:otherwise>
<xsl:number
value="count(preceding-sibling::row[child::cell[1][not(#padding)]]) + 1"
format="1. "/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="cell/text()"/>
</xsl:template>
</xsl:stylesheet>
I'm getting this result:
1. Title
a. Chapter
b. Chapter
c. Chapter
2. Title
d. Chapter
3. Title
e. Chapter
f. Chapter
4. Title
g. Chapter
What I would like is to have the "sub" items restart their numbering. I just can't figure how change the axis to stop looking back at earlier elements.
I was looking to have the "chapters" counted. So:
1. Title
a. Chapter
b. Chapter
c. Chapter
2. Title
a. Chapter
3. Title
a. Chapter
b. Chapter
4. Title
a. Chapter
I would suggest you try it this way:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<xsl:apply-templates select="row[not(cell/#padding)] | row/cell[#padding]"/>
</xsl:template>
<xsl:template match="row">
<xsl:number count="row[not(cell/#padding)]" format="1. "/>
<xsl:value-of select="cell"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="cell">
<xsl:number count="cell[#padding]" level="any" from="row[not(cell/#padding)]" format=" a. "/>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
I have "EOF Expexted" error in output XML after XSL transformation.
Question: What i need to do with this XSLT to get correct output?
Input XML:
<RowSet>
<Row>
<msg_id>1</msg_id>
<doc_id>1</doc_id>
<doc_version>1</doc_version>
</Row>
<Row>
<msg_id>2</msg_id>
<doc_id>1</doc_id>
<doc_version>2</doc_version>
</Row>
<Row>
<msg_id>3</msg_id>
<doc_id>1</doc_id>
<doc_version>3</doc_version>
</Row>
<Row>
<msg_id>4</msg_id>
<doc_id>2</doc_id>
<doc_version>1</doc_version>
</Row>
<RowSet>
XSLT:
<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:key name="kRowByDocId" match="Row" use="doc_id"/>
<xsl:template match="/*">
<xsl:apply-templates select=
"Row[generate-id()=generate-id(key('kRowByDocId', doc_id)[1])]"/>
</xsl:template>
<xsl:template match="Row">
<xsl:for-each select="key('kRowByDocId',doc_id)">
<xsl:sort select="doc_version" data-type="number" order="descending"/>
<xsl:if test="position() = 1"><xsl:copy-of select="."/></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output that i'm getting now (with EOF error):
<Row>
<msg_id>3</msg_id>
<doc_id>1</doc_id>
<doc_version>3</doc_version>
</Row>
<Row>
<msg_id>4</msg_id>
<doc_id>2</doc_id>
<doc_version>1</doc_version>
</Row>
Correct output must be something like this:
<RowSet>
<Row>
<msg_id>3</msg_id>
<doc_id>1</doc_id>
<doc_version>3</doc_version>
</Row>
<Row>
<msg_id>4</msg_id>
<doc_id>2</doc_id>
<doc_version>1</doc_version>
</Row>
<RowSet>
Thank you!
This is quite simple. If you want to output a Rowset element, you just to need to output one in your template that matches the root element
<xsl:template match="/*">
<RowSet>
<xsl:apply-templates select="Row[generate-id()=generate-id(key('kRowByDocId', doc_id)[1])]"/>
</RowSet>
</xsl:template>
Alternatively, if you don't want to hardcode the RowSet element, you can just use xsl:copy to copy whatever the root element is:
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="Row[generate-id()=generate-id(key('kRowByDocId', doc_id)[1])]"/>
</xsl:copy>
</xsl:template>