I've a global match on an attribut in my stylesheet but I want to exclude the f - element. How can I do that?
Example XML:
<a>
<b formatter="std">...</b>
<c formatter="abc">...</c>
<d formatter="xxx">
<e formatter="uuu">...</e>
<f formatter="iii">
<g formatter="ooo">...</g>
<h formatter="uuu">...</h>
</f>
</d>
</a>
Current solution:
<xsl:template match="//*[#formatter]">
...
</xsl:template>
I've tried something like this, but that didn't worked.
<xsl:template match="f//*[#formatter]">
...
</xsl:template>
<xsl:template match="//f*[#formatter]">
...
</xsl:template>
Either //f[#formatter] or f[#formatter] would have worked (the // is not necessary). When this XSLT is run on your example input:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[#formatter]">
<xsl:element name="transformed-{local-name()}">
<xsl:apply-templates select="#* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="f[#formatter]">
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>
The result is:
<a>
<transformed-b formatter="std">...</transformed-b>
<transformed-c formatter="abc">...</transformed-c>
<transformed-d formatter="xxx">
<transformed-e formatter="uuu">...</transformed-e>
<transformed-g formatter="ooo">...</transformed-g>
<transformed-h formatter="uuu">...</transformed-h>
</transformed-d>
</a>
As you can see, the f is excluded. Does this answer your issue, or have I misunderstood what you want to do?
Related
I am XSLT beginner, learning by example and by working on projects. Currently, I am working on creating grouped, nested structure from flat.
Consider this sample xml input:
<root>
<a>First text</a>
<b>Text</b>
<c>More text in c tag</c>
<d>There is even d tag</d>
<a>Another "a" test.</a>
<b>ěščřžýáíéúů</b>
<b>More b tags</b>
<c>One followed by c tag</c>
<a>Last a tag</a>
<b>This time only with b tag, but this goes on and on</b>
</root>
And this XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="utf-8"/>
<xsl:key name="groupA" match="b|c|d" use="generate-id(preceding-sibling::a[1])" />
<xsl:key name="groupB" match="c|d" use="generate-id(preceding-sibling::b[1])"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<wrapperTest>
<xsl:apply-templates/>
</wrapperTest>
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates select="#*|a"/>
<xsl:apply-templates select="#*|b"/>
</xsl:template>
<xsl:template match="a">
<xsl:copy>
<xsl:apply-templates select="key('groupA', generate-id())" />
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:apply-templates select="key('groupB', generate-id())" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The expected output is:
<wrapperTest>
<a>First text
<b>Text
<c>More text in c tag</c>
<d>There is even d tag</d>
</b>
</a>
<a>Another "a" test.
<b>ěščřžýáíéúů</b>
<b>More b tags
<c>One followed by c tag</c>
</b>
</a>
<a>Last a tag
<b>This time only with b tag, but this goes on and on</b>
</a>
</wrapperTest>
In the transformation I created are excessive copies created and I have no idea why. I guess that the isuue I am hiting upon is basic in its nature, but I cant figure it out.
The only limit for solution is, that preferably it should be in XSLT 1.0 (since the project is incorporated in python script with lxml). In the edge case, when this couldnt be achived with XSLT 1.0, I can accomodate for recent saxon version which removes any limitations ...
I have already looked at answers here, here and others, but most of them use either XSLT 2.0 or are very complicated for a beginner to knive through.
Final note: Ideally, proposed solution should be extensible in it nature, because the final form of my project should be also grouped by tag <c>, like so:
<wrapperTest>
<a>First text
<b>Text
<c>More text in c tag
<d>There is even d tag</d>
</c>
</b>
</a>
<a>Another "a" test.
<b>ěščřžýáíéúů</b>
<b>More b tags
<c>One followed by c tag</c>
</b>
</a>
<a>Last a tag
<b>This time only with b tag, but this goes on and on</b>
</a>
</wrapperTest>
Which I will happily do as learning excersize.
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:key name="b" match="b" use="generate-id(preceding-sibling::a[1])" />
<xsl:key name="cd" match="c|d" use="generate-id(preceding-sibling::b[1])"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<wrapperTest>
<xsl:apply-templates select="a"/>
</wrapperTest>
</xsl:template>
<xsl:template match="a">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="key('b', generate-id())" />
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="key('cd', generate-id())" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The recursive XSLT 2/3 for-each-group group-starting-with for all levels comes down to
<xsl:function name="mf:wrap" as="node()*">
<xsl:param name="input" as="node()*"/>
<xsl:for-each-group select="$input" group-starting-with="node()[node-name() = node-name($input[1])]">
<xsl:copy>
<xsl:sequence select="node(), mf:wrap(tail(current-group()))"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="root">
<xsl:copy>
<xsl:sequence select="mf:wrap(*)"/>
</xsl:copy>
</xsl:template>
https://xsltfiddle.liberty-development.net/gVhEaj8
Given the following source xml:
<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns="http://someorg.org">
<text>
<status value="generated"/>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Some text</p>
<p>Some text</p>
</div>
</text>
</Test>
I would like to have the same output as the above source xml (the source xml contains many other xml nodes but for this section, I want to output it as it is, with no changes.) I have the following xslt (see below) which strips the elements of their namespaces as desired. Unfortunately, it also strips the div elements of their name spaces but I want to retain them. The closest I got to achieving my aim is the following xslt but it outputs the div element twice because of the apply-templates but I only want the div element once with its namespace.
This is my xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://someorg.org"
xmlns="http://someorg.org"
exclude-result-prefixes="f xsl">
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match = "f:text/f:status">
<status value ="generated"/>
<div xmlns="http://www.w3.org/1999/xhtml">
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>
Instead of trying to remove namespaces for all elements (as handled by your <xsl:template match="*"> template, you can only target elements in the "http://someorg.org" namespace. Simply change the template match to this
<xsl:template match="f:*">
For the elements in the "http://www.w3.org/1999/xhtml" namespace, you could use the identity template to pick up everything else
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="http://someorg.org">
<xsl:output method="xml" indent="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="f:*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
add a template
<xsl:template match="*[local-name()='div'
and namespace-uri() = 'http://www.w3.org/1999/xhtml']">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
to perform a copy instead of creating a namespace-stripped element.
My xml and xslt file looks like follow. problem is when i apply my transformation file only 2nd one happens 1st one skips. How could i run both on first run. Please help Thanks.
//BEFORE TRANSFORMATION
<A>
<B>
<Name>ThisOne</Name>
<Target>abc</Target>
</B>
</A>
My XSLT FIle
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!--Transformation 1 to replace Target Text-->
<xsl:template match="A/B/Target/text()">
<xsl:text>xyz</xsl:text>
</xsl:template>
<!--Transformation 2 to Add a new node after Target-->
<xsl:template match="A/B/Target">
<xsl:copy-of select="."/>
<JOJO></JOJO>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
What i would like to see after transformation is following
<A>
<B>
<Name>ThisOne</Name>
<Target>xyz</Target>
<JOJO/>
</B>
</A>
Change
<xsl:template match="A/B/Target">
<xsl:copy-of select="."/>
<JOJO></JOJO>
</xsl:template>
to
<xsl:template match="A/B/Target">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
<JOJO></JOJO>
</xsl:template>
You could just use one template to rewrite the B node. Replace those two templates with this:
<xsl:template match ="A/B">
<B>
<Name><xsl:value-of select="Name"/></Name>
<Target>xyz</Target>
<JOJO/>
</B>
</xsl:template>
Can you set the position of a particular item in a for-each loop if the value equals something? I tried the below example but it didn't work:
<xsl:choose>
<xsl:when test='name = "Dining"'>
<xsl:value-of select="position()=1"/>
</xsl:when>
<xsl:otherwise>
[Normal position]
</xsl:otherwise>
</xsl:choose>
Dining will always appear at the top of the list and then the list will render as normal.
You haven't provided an example of your input XML, or shown exactly what you want to do with it, so I am guessing a bit. You could try something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="Dining"/>
<xsl:apply-templates select="*[not(self::Dining)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to the following XML:
<root>
<Bathroom />
<Dining />
<Kitchen />
<Bedroom />
</root>
It produces:
<root>
<Dining />
<Bathroom />
<Kitchen />
<Bedroom />
</root>
I need to add elements to an element, creating it first if it doesn't already exist.
My desired final result, after adding ABC and DEF, is:
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Q/>
<B>
<string>ABC</string>
<string>DEF</string>
</B>
<A>
I thought that the following would accomplish this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Insert a B element if it doesn't exist. -->
<xsl:template match="A[not(B)]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<B/>
</xsl:copy>
</xsl:template>
<xsl:template match="B">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<string>ABC</string>
<string>DEF</string>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If I start with the following, in which <B> already exists, it works fine, returning the result above:
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
<Q/>
<B/>
</A>
However, if I don't have a <B>, as in below:
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
<Q/>
</A>
then it creates the <B> as below, but doesn't insert ABC and DEF:
<?xml version="1.0" encoding="utf-8"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
<Q/>
<B/>
</A>
So, what am I missing? Thanks in advance.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Identity transform -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Insert a B element with string elements if it doesn't exist. -->
<xsl:template match="A[not(B)]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<B>
<xsl:call-template name="add-strings"/>
</B>
</xsl:copy>
</xsl:template>
<!-- Add string elements to existing B if missing -->
<xsl:template match="B[not(string)]">
<xsl:copy>
<xsl:call-template name="add-strings"/>
</xsl:copy>
</xsl:template>
<!-- Add string elements to caller -->
<xsl:template name="add-strings">
<string>ABC</string>
<string>DEF</string>
</xsl:template>
</xsl:stylesheet>
You will have to add the sub tags of B too when B does not exist, as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Identity transform -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Insert a B element if it doesn't exist. -->
<xsl:template match="A[not(B)]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<B>
<string>ABC</string>
<string>DEF</string>
</B>
</xsl:copy>
</xsl:template>
<xsl:template match="B">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<string>ABC</string>
<string>DEF</string>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when applied to
<?xml version="1.0" encoding="UTF-8"?>
<root>
<A>
<Q/>
</A>
<A>
<Q/>
<B/>
</A>
</root>
this gives
<?xml version="1.0" encoding="UTF-8"?>
<root>
<A>
<Q/>
<B>
<string>ABC</string>
<string>DEF</string>
</B>
</A>
<A>
<Q/>
<B>
<string>ABC</string>
<string>DEF</string>
</B>
</A>
</root>
Empo's answer was very close, but if <B> already contained <string> elements, the new <string>s weren't added. I made two minor changes, which solved that:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform. -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Insert a B element with string elements if it doesn't exist. -->
<xsl:template match="A[not(B)]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<B>
<xsl:call-template name="add-strings"/>
</B>
</xsl:copy>
</xsl:template>
<!-- Add string elements to existing B element. -->
<xsl:template match="B"> <!-- Whether there are <string>s or not. -->
<xsl:copy>
<xsl:apply-templates select="#* | node()"/> <!-- Keep existing <string>s. -->
<xsl:call-template name="add-strings"/>
</xsl:copy>
</xsl:template>
<!-- Add string elements to caller. -->
<xsl:template name="add-strings">
<string>ABC</string>
<string>DEF</string>
</xsl:template>
</xsl:stylesheet>