Input XML:
<derivatives>
<derivative id="4" name="Audio Content">
<operator id="1" name="Reliance">
<referenceCode code="62033815">
<mobileCircle id="1" name="Maharashtra"/>
</referenceCode>
</operator>
<operator id="22" name="Aircel">
<referenceCode code="811327">
<mobileCircle id="1" name="Maharashtra"/>
</referenceCode>
</operator>
</derivative>
</derivatives>
Expected Output XML:
<hellotune>
<operator>Aircel</operator>
<vcode>811327</vcode>
</hellotune>
Current output (which is wrong):
<hellotune>
<operator>Aircel</operator>
<vcode/>
</hellotune>
XSL (which is not working):
<xsl:if test="derivatives/derivative/operator[#name='Aircel']">
<hellotune>
<operator>Aircel</operator>
<vcode><xsl:value-of select="referenceCode/#code"/></vcode>
</hellotune>
</xsl:if>
Note: Using XSL v1.0. Not mentioned the complete XSL for brevity.
Based on the XSL you provided, it can be presumed that the context node is the root node, but starting from the root node, the path referenceCode/#code does not match anything in your input. Appending derivatives/derivative/operator/ before that path would succeed in finding a referenceCode #code attribute, but it would find the wrong one. Try this push-style approach:
<xsl:template match="/">
<xsl:apply-templates select="derivatives/derivative/operator[#name='Aircel']" />
</xsl:template>
<xsl:template match="operator">
<hellotune>
<operator><xsl:value-of select="#name" /></operator>
<vcode><xsl:value-of select="referenceCode/#code"/></vcode>
</hellotune>
</xsl:template>
The xpath xpression in the element <vcode> is not referring to any node in the input document. The best way to match all the nodes is to use
//
like in your code you can use
//referenceCode/#code
(but this is only for information purpose and using same can't get your result).
you can try this way:
<xsl:template match="/">
<hellotune>
<xsl:for-each select="//operator">
<xsl:if test="./#name='Aircel'">
<operator><xsl:value-of select="#name"/></operator>
<vcode><xsl:value-of select="referenceCode/#code"/></vcode>
</xsl:if>
</xsl:for-each>
</hellotune>
</xsl:template>
Hope this helps :)
Related
I think it's a very simple question. But although I build very fancy xslt transformation, this simple one cannot be solved by me.
The problem is:
I want to add attributes to xsl-fo nodes, depending on xml data. These attributes have often a hyphen in it. How can I add these with an xslt transformation where xsl:attributes doesn't like the hyphenation character.
In a xml node I have got two attributes (name and value)
Example: name="font_size", value="7pt"
<Report>
<text content="I am a text">
<blockFormat name="font_size" value="7pt" />
</text>
</Report>
(I understand this is not wanted because you want to work with styles etceters. It's just an example with a simplified problem)
Now I want to make a xsl-fo block, and I want to place that attributes in the block element by using the xsl-function xsl:attribute
<fo:block>
<attribute name="{replace(#name,'_','-')}" select="#value" />
....
</fo:block>
goal to achieve after transformation
<fo:block font-size="7pt">
....
</fo:block
It doesn't function and I think this is because in xslt I can't put an hyphen in the attribute name, but in the fo-attribute it is needed.
Is there a way to use the xsl:attribute function for this?
And when not, what kind of working around do you suggest.
Thank you for helping!!!!
There are 1000 ways to do it, here is one (I didn't do anything with your Report element):
Input:
<Report>
<text content="I am a text">
<blockFormat name="font_size" value="7pt" />
</text>
</Report>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
version="1.0">
<xsl:template match="Report">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="text">
<fo:block>
<xsl:apply-templates select="blockFormat/#*"/>
<xsl:value-of select="#content"/>
</fo:block>
</xsl:template>
<xsl:template match="#name">
<xsl:attribute name="{translate(.,'_','-')}">
<xsl:value-of select="ancestor::blockFormat/#value"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="#value"/>
</xsl:stylesheet>
Output:
<Report>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="7pt">I am a text</fo:block>
</Report>
Use #select instead of #value:
<fo:block>
<attribute name="{replace(#name,'_','-')}" select="#value" />
....
</fo:block>
See https://www.w3.org/TR/xslt20/#creating-attributes
Also, you need to be using XSLT 2.0 or 3.0 to use #select. If you're using XSLT 1.0, you'd have to do it as xsl:attribute/xsl:value-of/#select.
(It would also have helped understanding of your problem if you'd also shown the wrong result that you were getting.)
The variable $authors is declared as follows:
<listAuthor>
<author catalogNumber="26A.18.1" fullName="Pjotr Domanski"/>
<author catalogNumber="26A.19.1" fullName="Hermine Rex"/>
<author catalogNumber="26A.19.2" fullName="Christoferus Hohenzell"/>
</listAuthor>
Further, the following XML input is given:
<h1:Block Type="obj">
<h1:Field Type="5000" Value="77772001" />
<h1:Field Type="5209" Value="26A.19.1 : Lazy Tennis"/>
</h1:Block>
The expected output is:
<h1:Block Type="obj">
<h1:Field Type="5000" Value="77772001" />
<h1:Field Type="5209" Value="26A.19.1 : Lazy Tennis"/>
<h1:Field Type="9904" Value="Hermine Rex"/>
</h1:Block>
Here is a snippet of my transformation:
<xsl:template match="h1:Block">
<h1:Block Type="obj">
<xsl:copy-of select="child::*"/>
<h1:Field Type="9904">
<xsl:attribute name="Value"
select="$authors/listAuthor/author[contains (h1:Field[#Type='5209']/#Value, #catalogNumber)]/#fullName"/>
</h1:Field>
</h1:Block>
</xsl:template>
The name of the author should be put in h1:Field[#Type='9904']/#Value, when #catalogNumber in $authors/author is contained in h1:Field[#Type='5209']. Unfortunately, the attribute #Value remains empty.
I found the following workaround by introducing a variable:
<xsl:template match="h1:Block">
<h1:Block Type="obj">
<xsl:copy-of select="child::*"/>
<xsl:variable name="value5209" select="h1:Field[#Type='5209']/#Value"/>
<h1:Field Type="9904">
<xsl:attribute name="Value"
select="$authors/listAuthor/author[contains ($value5209, #catalogNumber)]/#fullName"/>
</h1:Field>
</h1:Block>
</xsl:template>
In this case, it works fine. Does anyone have any idea why it doesn't work in the first case? I use Saxon Saxon-HE 9.6.
Inside the predicate the context node is the author element which does not have a h1:Field, so change $authors/listAuthor/author[contains (h1:Field[#Type='5209']/#Value, #catalogNumber)]/#fullName to $authors/listAuthor/author[contains (current()/h1:Field[#Type='5209']/#Value, #catalogNumber)]/#fullName to select the context node of the template.
I have the following data:
<books>
<entry id="8">
<author name="tony-blair">Tony Blair</author>
</entry>
<entry id="9">
<author name="william-campbell">William Campbell</author>
</entry>
</books>
And use the following template
<xsl:template match="books/entry">
<xsl:value-of select="author"/>
<xsl:value-of select="ancestor::books/entry/#id"/>
</xsl:template>
I try to use ancestor::books/entry/#id but it results only the first id.
How to get the parent entry id while we are in the current position entry?
<xsl:template match="books/entry">
<xsl:value-of select="author"/>
<xsl:value-of select="#id"/>
</xsl:template>
Within a
<xsl:template match="books/entry">
the current context node is the entry element, so you can just use
<xsl:value-of select="#id" />
You don't need to go up to the books element and back down again.
<xsl:template match="entry">
<xsl:value-of select="author"/>
<xsl:value-of select="#id"/>
</xsl:template>
As others have said, you don't need to go up and down the tree.
books/entry is a tiny performance optimisation but it seems you're not there yet, so keep things simple and you're probably not processing massive documents anyway.
I am using xslt2.0 for convert one xml format to another xml format. This is my sample xml document.
<w:document>
<w:body>
<w:p>Para1</w:p>
<w:p>Para2</w:p>
<w:p>Para3</w:p>
<w:p>Para4</w:p>
</w:body>
</w:document>
Initially this is my xml format.so, i handled each and every <w:p> elements through my function in xslt given below...
<xsl:template match="document">
<Document>
<xsl:sequence select="mf:group(body/p, 1,count(//w:body//w:p)-1)"/>
</Document>
</xsl:template>
So,In that xslt function, i have coded how to reformat those elements.It's working fine...
But now,Xml format is restructured like given below...
<w:document>
<w:body>
<w:tbl><!--some text with children elements--></w:tbl>
<w:tbl><!--some text with children elements--></w:tbl>
<w:p>Para1</w:p>
<w:p>Para2</w:p>
<w:p>Para3</w:p>
<w:p>Para4</w:p>
</w:body>
</w:document>
So, As of now i have to handle both and elements in a same sequence.....
What i want to do is,
If i encounter elemtents then i have to call my template given below...
<xsl:template match="document">
<Document>
<xsl:for-each select="w:tbl">
<xsl:apply-templates select="w:tbl">
</xsl:apply-templates>
</xsl:for-each>
<xsl:sequence select="mf:group(body/p, 1,count(//w:body//w:p)-1)"/>
</Document>
</xsl:template>
<xsl:template match="w:tbl">
<!--xslt code here -->
</xsl:template>
But the for-each statement is not executed when I trying transformation...
So, Please guide me to get out of this issue...
I think instead of
<xsl:template match="document">
<Document>
<xsl:for-each select="w:tbl">
<xsl:apply-templates select="w:tbl">
</xsl:apply-templates>
</xsl:for-each>
<xsl:sequence select="mf:group(body/p, 1,count(//w:body//w:p)-1)"/>
</Document>
</xsl:template>
you simply want
<xsl:template match="document">
<Document>
<xsl:apply-templates select="w:body/w:tbl"/>
<xsl:sequence select="mf:group(body/p, 1,count(//w:body//w:p)-1)"/>
</Document>
</xsl:template>
If that does not do what you want then please show the result you want.
ORIGINAL - this is my original XML:
<course acad_year="2012" cat_num="85749" offered="N" next_year_offered="2013">
<term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
<department code="VES">
<dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
<dept_short_name>Visual and Environmental Studies</dept_short_name>
</department>
</course>
DESIRED RESULT - I am trying to create another shorter version of the original XML that will group and list all department codes like in the example below:
<departments>
<department code="some_code" name="some_name"/>
<department code="some_code" name="some_name"/>
<department code="some_code" name="some_name"/>
</departments>
This is what I am trying and not working:
<xsl:template match="/">
<departments>
<xsl:for-each-group select="fas_courses/course" group-by="department[#code]">
<xsl:text disable-output-escaping="yes"> <department code=" </xsl:text>
<xsl:value-of select="department/#code"/>
<xsl:text>" name="</xsl:text>
<xsl:value-of select="dept_short_name"/>
<xsl:text disable-output-escaping="yes">"><department/></xsl:text>
</xsl:for-each-group>
</departments>
</xsl:template>
The error I am getting
F [Saxon-PE 9.4.0.3] The value of attribute "code" associated with an element type "department" must not contain the '<' character.
From the error message, I understand the '<' character is giving me an error inside of the xsl:text element, but how do I put that character then?, I am already using disable-output-escaping="yes", is there anything else???
Thanks!
Using disable-output-escaping is never a good idea, and you really don't need it here. Try this:
<xsl:template match="/">
<departments>
<xsl:for-each-group select="fas_courses/course" group-by="department/#code">
<department code="{department/#code}" name="{department/dept_short_name}" />
</xsl:for-each-group>
</departments>
</xsl:template>
You need to write your text inside <[DATA[...]]> so it doesn't get parsed or use < and > for the tags that you output.