Input XML:
<root>
<recordList>
<record priref="1">
<Group attr="val">
<Field1>Value X</Field1>
<Field2>
<value lang="en-US">Foo</value>
<value lang="de-DE">Bar</value>
</Field2>
</Group>
<Field3 attr="val">Value Y</Field3>
</record>
<record priref="2">
<Field3 attr="val">Value Z</Field3>
</record>
</recordList>
</root>
Desired output (kind of a "shallow copy" with only the immediate child elements and attributes):
<root>
<record priref="1">
<Group attr="val" />
<Field3 attr="val">Value Y</Field3>
</record>
<record priref="2">
<Field3 attr="val">Value Z</Field3>
</record>
</root>
Is there another way (e.g. without for-each) to achieve this?
<xsl:template match="/">
<root>
<xsl:apply-templates select="root/recordList/record" />
</root>
</xsl:template>
<xsl:template match="record">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:for-each select="*">
<xsl:copy>
<xsl:copy-of select="#* | text()"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
edit: the text nodes "Value Y" and "Value Z" are actually supposed to be in the result. "Foo" and "Bar" are still not desired anywhere in the result.
How about simply not apply templates to nodes you don't want?
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="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="recordList">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="record/*">
<xsl:copy>
<xsl:apply-templates select="#*|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
One way is to not process any grandchildren:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="recordList">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="record/*/node()"/>
</xsl:transform>
A very short template uses the parent::-axis:
<xsl:template match="root|record|*[parent::record]|*[parent::record]/text()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" />
It's output is
<?xml version="1.0"?>
<root>
<record priref="1">
<Group attr="val">
</Group>
<Field3 attr="val">Value Y</Field3></record>
<record priref="2">
<Field3 attr="val">Value Z</Field3>
</record>
</root>
Related
<DETAILS>
<PUT>
<RECORD>ABC_PQRST0123456-001_1</RECORD>
<NUMBER>4</NUMBER>
<INST>1,2</INST>
</PUT>
<PUT>
<RECORD>ABC_PQRST0123456-001_2</RECORD>
<NUMBER>1</NUMBER>
</PUT>
</DETAILS>
How to remove the other loop elements from where INST don't have values.Can someone help me with xslt Transformation code to sort this
<PUT>
<RECORD>ABC_PQRST0123456-001_2</RECORD>
<NUMBER>1</NUMBER>
</PUT>
This does what you describe:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="PUT[not(INST)]">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<!-- match but dont do anything -->
<xsl:template match="PUT">
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I'm using a for-each in my XSLT template.
This is my example input XML:
<products>
<data>
<label_1>some_label1</label_1>
<label_2>some_label2</label_2>
<values>
<a>a</a>
<b>b</b>
</values>
</data>
<data>
<label_1>some_label1</label_1>
<label_2>some_label2</label_2>
<values>
<c>c</c>
<d>d</d>
</values>
</data>
</products>
Now based on my template:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http:/example.com/ns">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="data">
<data>
<xsl:variable name="values" select="values" />
<xsl:for-each select="$values">
<xsl:apply-templates select="#*|node()" />
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
I get only <values></values> and that is ok for me.
That's my output:
<products>
<data>
<a>a</a>
<b>b</b>
</data>
<data>
<c>c</c>
<d>d</d>
</data>
</products>
What i need in my output is namespace like this:
<products>
<data>
<ns:a>a</ns:a>
<ns:b>b</ns:b>
</data>
<data>
<ns:c>c</ns:c>
<ns:d>d</ns:d>
</data>
</products>
So what i understand is "each element of values is applied by template". How can I add namespace ?
You can get output similar to what you show (albeit well-formed) by using:
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="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="data">
<xsl:copy>
<xsl:apply-templates select="values/*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="values/*">
<xsl:element name="ns:{local-name()}" namespace="http:/example.com/ns">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Or, if you prefer:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http:/example.com/ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/products">
<products>
<xsl:for-each select="data">
<xsl:copy>
<xsl:for-each select="values/*">
<xsl:element name="ns:{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>
Replace http:/example.com/ns with your own namespace URI.
Credits
This answer follows the technique used in this SO answer to a similar problem.
Solution
Add namespace information to all descendants of specific elements. Augment the stylesheet by a template matching this set of nodes:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://my.ns.uri"
>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="data">
<data>
<xsl:variable name="values" select="values" />
<xsl:for-each select="$values">
<xsl:apply-templates select="#*|node()" />
</xsl:for-each>
</data>
</xsl:template>
<!--
Added template.
-->
<xsl:template match="data//*">
<xsl:element name="ns:{name()}" namespace="http://my.ns.uri">
<xsl:for-each select=".">
<xsl:apply-templates select="#*|node()" />
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I have this requirement that I only need to populate the <Group> tag under the <Section>, if the <Group> tag under the <Data> is not present. I can't get the correct output that I want. For example:
INPUT
<Record>
<Data>
<ID>1234DFD57</ID>
<Group>
<abc-KD>243fds</abc-KD>
</Group>
<Section>
<ID>33-2311</ID>
<Group>
<abc-KD>NORM</abc-KD>
</Group>
<Date>2017-03-25</Date>
</Section>
<Date>2017-03-25</Date>
</Data>
</Record>
EXPECTED OUTPUT
<Record>
<Data>
<ID>1234DFD57</ID>
<Group>
<abc-KD>243fds</abc-KD>
</Group>
<Section>
<ID>33-2311</ID>
<Date>2017-03-25</Date>
</Section>
<Date>2017-03-25</Date>
</Data>
</Record>
XSLT:
<xsl:stylesheet version="2.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="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Section">
<xsl:copy>
<xsl:copy-of select="ID"/>
<xsl:if test="normalize-space(string(../Group)) = ''">
<xsl:copy-of select="Group"/>
</xsl:if>
<xsl:copy-of select="Date"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Your feedback is highly appreciated.
Regards,
Your current stylesheet does the work. More efficient way would be:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- do nothing for the Group -->
<xsl:template match="Section[normalize-space(parent::Data/Group) != '']/Group"/>
</xsl:stylesheet>
The identity transform template is the one that copies every node in the xml, while recursively processing them in document order, as it is.
The second template matches the Group elements with the desired condition and does nothing for it, hence omitting them in the output.
The x-path in the #match does the trick:
Section[normalize-space(parent::Data/Group) != '']/Group
It matches those Section/Group elements under Data whose Group doesn't exist or has null value(excluding space characters).
I tried all the codes I've seen in the internet with relevant requirement as I have. However, in my case, I also need to populate the namespace within the inner parent group. My XSLT didn't work as expected. Can anyone help me with this? Thank you.
XSLT CODE:
<xsl:stylesheet version="2.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="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Section">
<Section xmlns="www.hdgd.co">
<xsl:apply-templates select="#*|node()"/>
</Section>
</xsl:template>
INPUT:
<Record xmlns="www.hdgd.co">
<Data>
<Section>
<ID>1234DFD57</ID>
</Section>
</Data>
EXPECTED OUTPUT:
<Record>
<Data>
<Section xmlns="www.hdgd.co">
<ID>1234DFD57</ID>
</Section>
</Data>
GENERATED OUTPUT:
<Record xmlns="www.hdgd.co">
<Data>
<Section>
<ID>1234DFD57</ID>
</Section>
</Data>
You seem to be unaware of namespaces inheritance. The default namespace declaration at the Record root element is applied to all elements of the input document. Therefore, in order to achieve the requested result, you must take all elements out of their namespace, while leaving the Section element and its descendants unprocessed:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="www.hdgd.co">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Section">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Added:
If your input has attributes that need copying, then change the first template to:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
That sounds as if you want to remove the namespace from Record and Data:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xpath-default-namespace="www.hdgd.co">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record | Data">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
</xsl:transform>
http://xsltransform.net/bEzjRJM gives
<?xml version="1.0" encoding="UTF-8"?><Record>
<Data>
<Section xmlns="www.hdgd.co">
<ID>1234DFD57</ID>
</Section>
</Data>
</Record>
I am trying to remove all empty elements after I have finished the transformation however I am just not coming right.
I have the following XML
<root>
<record name='1'>
<Child1>value1</Child1>
<Child2>value2</Child2>
</record>
<record name='2'>
<Child1>value1</Child1>
<Child2>value2</Child2>
</record>
<record name='3'>
<Child1>value1</Child1>
<Child2>value2</Child2>
</record>
</root>
and I want the output to be
<root>
<record name="1">
<Element>1</Element>
</record>
</root>
however I keep getting all the empty record elements as well and I can't figure out how to get rid of them.
<root>
<record>
<Element>1</Element>
</record>
<record/>
<record/>
</root>
This is my stylesheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//record">
<xsl:copy>
<xsl:call-template name="SimpleNode"/>
</xsl:copy>
</xsl:template>
<xsl:template name="SimpleNode">
<xsl:if test="#name = '1'">
<Element><xsl:value-of select="#name"/></Element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I would rewrite your XSLT a little to match record elements differently depending on the value of their #name attribute.
The following XSLT stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Only produce output for record elements where the name attribute is 1. -->
<xsl:template match="record[#name='1']">
<xsl:copy>
<element>
<xsl:value-of select="#name"/>
</element>
</xsl:copy>
</xsl:template>
<!-- For every other record attribute, output nothing. -->
<xsl:template match="record"/>
</xsl:stylesheet>
produces the following output when applied to your example input XML:
<root>
<record>
<element>1</element>
</record>
</root>