I have a html page for an example like below
<html><head><title>test</title></head><body><div>test1</div><footer><div>test2</div></footer></body></html>
I have written xslt 1.0 to transform and extract the title and body content, but my requirement is to ignore footer content alone and consider all other element values inside body content. How to achieve this ?
<xsl:template match="/">
<document >
<xsl:copy-of select="#*" />
<xsl:apply-templates select="html/head" />
<xsl:apply-templates select="html/body" />
</document>
</xsl:template>
<xsl:template match="html/head">
<content name="title">
<xsl:value-of select="title" />
</content>
</xsl:template>
<xsl:template match="html/body">
<content name="snippet">
<xsl:value-of select="viv:replace(viv:replace(.,'<[^>]*>',' ', 'gi'),' ','','gi')"/>
</content>
</xsl:template>
Q: how to exclude footer element content from body using xslt 1.0
If this is really your question this should be answered hundred times.
Stat with an identity transform and have empty templates for elements to ignore.
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body/footer"/>
Looking to your xlst let us assume there a some strange_other_things requested.
Do strange_other_things for the body without footer put the result form identity transfer into a variable.
<xsl:template match="body" mode="strange_other_things">
<xsl:variable name="body" >
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:variable>
<!-- use $body but I'm out here -->
</xsl:template>
Further guess: With viv:replace(.,'<[^>]*>',' ', 'gi') you try to remove xml element names. This will not work because . is used in a text context an will only return all text inside the current node.
So if I'm right the question is a lite deceptive.
Related
Kindly help me to wrap the img.inline element with the following sibling text comma (if comma exists):
text <img id="1" class="inline" src="1.jpg"/> another text.
text <img id="2" class="inline" src="2.jpg"/>, another text.
Should be changed to:
text <img id="1" class="inline" src="1.jpg"/> another text.
text <span class="img-wrap"><img id="2" class="inline" src="2.jpg"/>,</span> another text.
Currently, my XSLT will wrap the img.inline element and add comma inside the span, now I want to remove the following comma.
text <span class="img-wrap"><img id="2" class="inline" src="2.jpg"/>,</span>
, <!--remove this extra comma--> another text.
My XSLT:
<xsl:template match="//img[#class='inline']">
<xsl:copy>
<xsl:choose>
<xsl:when test="starts-with(following-sibling::text(), ',')">
<span class="img-wrap">
<xsl:apply-templates select="node()|#*"/>
<xsl:text>,</xsl:text>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|#*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
<!-- checking following-sibling::text() -->
<xsl:apply-templates select="following-sibling::text()" mode="commatext"/>
</xsl:template>
<!-- here I want to match the following text, if comma, then remove it -->
<xsl:template match="the following comma" mode="commatext">
<!-- remove comma -->
</xsl:template>
Is my approach is correct? or is this something should be handled differently? pls suggest?
Currently you are copying the img and the embedding the span within that. Also, you do <xsl:apply-templates select="node()|#*"/> which will select child nodes of img (or which there are none). And for the attributes it will end add them to the span.
You don't actually need the xsl:choose here as you can add the condition to the match attribute.
<xsl:template match="//img[#class='inline'][starts-with(following-sibling::node()[1][self::text()], ',')]">
Note I have changed the condition as following-sibling::text() selects ALL text elements that follow the img node. You only want to get the node immediately after the img node, but only if it is a text node.
Also, trying to select the following text node with xsl:apply-templates is probably not the right approach, assuming you have a template that matches the parent node which selects all child nodes (not just img ones). I am assuming you were using the identity template here.
Anyway, try this XSLT instead
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="no" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//img[#class='inline'][starts-with(following-sibling::node()[1][self::text()], ',')]">
<span class="img-wrap">
<xsl:copy-of select="." />
<xsl:text>,</xsl:text>
</span>
</xsl:template>
<xsl:template match="text()[starts-with(., ',')][preceding-sibling::node()[1][self::img]/#class='inline']">
<xsl:value-of select="substring(., 2)" />
</xsl:template>
</xsl:stylesheet>
I use a tool where a xslt template is pre-defined and it is not desirable to remove it.
<xsl:template match="/">
<Msg xmlns="urn:com.sap.b1i.vplatform:entity">
<xsl:copy-of select="/vpf:Msg/#*"></xsl:copy-of>
<xsl:copy-of select="/vpf:Msg/vpf:Header"></xsl:copy-of>
<Body>
<xsl:copy-of select="/vpf:Msg/vpf:Body/*"></xsl:copy-of>
<Payload Role="X" id="{$atom}">
<xsl:call-template name="transform"></xsl:call-template>
</Payload>
</Body>
</Msg>
<xsl:template name="transform">
<!-- In this area we write our xpath and build the xml-file-->
</xsl:template>
Now I want to use the Muenchian grouping method. But for this method you also need to define a template en key. Like this:
<xsl:key name="KeyOrder" match="/vpf:Msg/vpf:Body/vpf:Payload[#id='atom8']/Orders/jdbc:Row" use="jdbc:RecId2" />
<xsl:template match="Orders" >
<Documents>
<xsl:for-each select="jdbc:Row[count(. | key('KeyOrder', jdbc:RecId2)[1]) = 1]">
<xsl:sort select="jdbc:RecId2" />
<Document>
<xsl:copy-of select="jdbc:RecId2" />
<xsl:for-each select="key('KeyOrder', jdbc:RecId2)">
<xsl:sort select="jdbc:OrderNrRef" />
<xsl:copy-of select="." />
</xsl:for-each>
</Document>
</xsl:for-each>
</Documents>
</xsl:template>
The problem is that the 2 templates won't work togetheter the way I copied it here. That means, I don't get the Muenchian grouping results. It's only works when I 'disable' xsl:template match="/", but then I lose a lot of other information which is necessary further in the process.
So how can I get in my XML file the results of both templates?
Within the same XSLT (2.0) Stylesheet and transformation I would like to:
1) first preprocess the whole XML Datasource (Add a attribute
with a specific calculation to certain elements)
and then
2: transform the changed XML Datasource with the sylesheet's templates.
How can I achieve this? A code example would be nice?
Yes it is possible. One possibility would be to to proceed as follows:
<xsl:template match="/">
<!-- store the modified content in a variable -->
<xsl:variable name="preprocessed.doc">
<xsl:apply-templates mode="preprocess" />
</xsl:variable>
<!-- process the modified contents -->
<xsl:apply-templates select="$preprocessed.doc/*" />
</xsl:template>
<!-- first pass: sample process to add an attribute named "q" -->
<xsl:template match="*" mode="preprocess">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:attribute name="q"><xsl:number count="*" level="any" /></xsl:attribute>
<xsl:apply-templates mode="preprocess" />
</xsl:copy>
</xsl:template>
<!-- "normal" processing of the modified content. It is able to used the newly processed attribute. -->
<xsl:template match="*[#q <= 5]">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*"/>
In the first pass, an attribute is added, counting the element in the input XML.
In the processing, we only retain the elements having the value of the q attribute set a number less or equals to 5.
I am struggling with xslt from the past 2 days, owing to my starter status.My requirement is that given any input XML file ,I want the output to be a list of all the XPaths of all the tags in order in which they appear in the original XML document(parent, then parent,parents Attributes list/child, parent/child/childOFchild and so forth). THe XSLT should not be specific to any single XMl schema. It should work for any XML file, which is a valid one.
Ex:
If the Input XML Is :
<v1:Root>
<v1:UserID>test</v1:UserID>
<v1:Destination>test</v1:Destination>
<v1:entity name="entiTyName">
<v11:attribute name="entiTyName"/>
<v11:attribute name="entiTyName"/>
<v11:attribute name="entiTyName"/>
<v11:filter type="entiTyName">
<v11:condition attribute="entiTyName" operator="eq" value="{FB8D669E-D090-E011-8F43-0050568E222C}"/>
<v11:condition attribute="entiTyName" operator="eq" value="1"/>
</v11:filter>
<v11:filter type="or">
<v11:filter type="or">
<v11:filter type="and">
<v11:filter type="and">
<v11:condition attribute="cir_customerissuecode" operator="not-like" value="03%"/>
</v11:filter>
</v11:filter>
</v11:filter>
</v11:filter>
</v1:entity>
</v1:Root>
I want my output to be :
/v1:Root/v1:UserID
/v1:Root/v1:Destination
/v1:Root/v1:entity/#name
/v1:Root/v1:entity/v11:attribute
/v1:Root/v1:entity/v11:attribute/#name
/v1:Root/v1:entity/v11:attribute[2]
/v1:Root/v1:entity/v11:attribute[2]/#name
/v1:Root/v1:entity/v11:attribute[3]
/v1:Root/v1:entity/v11:attribute[3]/#name
/v1:Root/v1:entity/v11:filter/#type
/v1:Root/v1:entity/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter/v11:condition/#attribute
/v1:Root/v1:entity/v11:filter/v11:condition/#operator
/v1:Root/v1:entity/v11:filter/v11:condition/#value
/v1:Root/v1:entity/v11:filter/v11:condition[2]
/v1:Root/v1:entity/v11:filter/v11:condition[2]/#attribute
/v1:Root/v1:entity/v11:filter/v11:condition[2]/#operator
/v1:Root/v1:entity/v11:filter/v11:condition[2]/#value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/#type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/#type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/#type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/#attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/#operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition/#value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/#type
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/#attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/#operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition/#value
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/#attribute
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/#operator
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]/#value
So, it is basically all the XPath of each element ,then the Xpath of the elements Attributes.
I have an XSLT with me, which is like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" />
<xsl:template match="*[not(child::*)]">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/', name())" />
<xsl:if test="count(preceding-sibling::*[name() = name(current())]) != 0">
<xsl:value-of
select="concat('[', count(preceding-sibling::*[name() = name(current())]) + 1, ']')" />
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
</xsl:stylesheet>
THe output which gets Produced does not cater to complex tags and also the tag's attributes in the resulting Xpath list :(.
Kindly help me in fixing this xslt to produce the output as mentioned above.
THe present output from the above XSLT is like this :
/v1:Root/v1:UserID
/v1:Root/v1:Destination
/v1:Root/v1:entity/v11:attribute
/v1:Root/v1:entity/v11:attribute[2]
/v1:Root/v1:entity/v11:attribute[3]
/v1:Root/v1:entity/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter/v11:filter/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition[2]
/v1:Root/v1:entity/v11:filter[2]/v11:filter[2]/v11:filter[2]/v11:condition[3]
I think there's a discrepancy between your sample input and output, in that the output describes a filter element with two conditions that's not in the source XML. At any rate, I believe this works:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" />
<!-- Handle attributes -->
<xsl:template match="#*">
<xsl:apply-templates select="ancestor-or-self::*" mode="buildPath" />
<xsl:value-of select="concat('/#', name())"/>
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Handle non-leaf elements (just pass processing downwards) -->
<xsl:template match="*[#* and *]">
<xsl:apply-templates select="#* | *" />
</xsl:template>
<!-- Handle leaf elements -->
<xsl:template match="*[not(*)]">
<xsl:apply-templates select="ancestor-or-self::*" mode="buildPath" />
<xsl:text>
</xsl:text>
<xsl:apply-templates select="#*" />
</xsl:template>
<!-- Outputs a path segment for the matched element: '/' + name() + [ordinalPredicate > 1] -->
<xsl:template match="*" mode="buildPath">
<xsl:value-of select="concat('/', name())" />
<xsl:variable name="sameNameSiblings" select="preceding-sibling::*[name() = name(current())]" />
<xsl:if test="$sameNameSiblings">
<xsl:value-of select="concat('[', count($sameNameSiblings) + 1, ']')" />
</xsl:if>
</xsl:template>
<!-- Ignore text -->
<xsl:template match="text()" />
</xsl:stylesheet>
Is there any way to optimize this code.
<xsl:choose>
<xsl:when test="val1 = val2">
<xsl:apply-templates select="node"/>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:apply-templates select="node"/>
</div>
</xsl:otherwise>
</xsl:choose>
I do not like having to write twice the same <xsl:apply-templates select="node"/>.
Update:
The idea is that depending on the result of comparison to do one of two things:
Just print some information (which we obtain after applying the template <xsl:apply-templates select="node"/>).
Print the same information, but in advance "wrapping" it in the container (div for example).
Use:
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
Here is a complete example. This 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:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<t>
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
</t>
</xsl:template>
<xsl:template match="node[not(val1 = val2)]">
<div>
<node>
<xsl:apply-templates/>
</node>
</div>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>
<node>
<val1>1</val1>
<val2>2</val2>
</node>
<node>
<val1>3</val1>
<val2>3</val2>
</node>
</t>
produces the wanted, correct result:
<t>
<node>
<val1>3</val1>
<val2>3</val2>
</node>
<div>
<node>
<val1>1</val1>
<val2>2</val2>
</node>
</div>
</t>
Explanation of the solution:
Whenever an <xsl:apply-templates> has an <xsl:sort> child, the nodes that are selected are sorted according the data provided in the <xsl:sort> child(ren) and the results of applying templates on each selected node appear in the output in that (sort) order -- not in document order.
In the transformation above we have:
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
This means that the results of applying templates to the elements named node for which it is true that val1=val2 will appear before the results of applying templates to the elements named node for which val1=val2 is not true. This is because false sorts before true.
If this explanation is not clear, the reader is directed to read more about <xsl:sort>.
This is hardy possible for that simple example, but if the amount of the wrapping code and the duplicate code are worth bothering, you have two options:
introduce a chain of templates with different modes:
<!-- instead of choose -->
<xsl:apply-template match="." mode="container" />
...
<xsl:template match="container" mode="container">
<xsl:apply-templates select="." mode="dup-group"/>
</xsl:template>
<xsl:template match="container[val1=val2]" mode="container">
<div>
<xsl:apply-templates select="." mode="dup-group"/>
</div>
</xsl:template>
<xsl:template match="container" mode="dup-group">
<xsl:apply-templates select="node"/>
<!-- other duplicate code -->
...
</xsl:template>
move a part of code into a separate imported stylesheet and override the template rule in the current stylesheet:
<xsl:template match="container[val1=val2]">
<div>
<xsl:apply-imports />
</div>
</xsl:template>
The xslt you are posting here is not very optimizable. If the code that you don't want to duplicate would be more than two lines, you could optimize by creating a named template and calling that instead. In this example it really doesn't make much sense, but you'll get the idea:
<xsl:when test="val1=val2">
<xsl:call-template name="optimized"/>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:call-template name="optimized"/>
</div>
</xsl:otherwise>
And the template:
<xsl:template name="optimized">
<xsl:apply-templates select="node"/>
</xsl:template>
The called template is in the same context as the calling code, so you can just copy the code you don't want to duplicate to the named template.