Is it possible to use the command on attributes? I want this to be able to run without knowing the attribute names. Here's a quick (terrible) example:
<candy hard="true" soft="false" stripes="true" solid="false">
In my head (this doesn't work) it should look something like this:
<xsl:for-each select="candy/#[#='true']">
Is there a way around this to run through attributes without knowing their name, or do I need to write each attribute being looked at?
Edit
Heres an example of me trying to create a variable out of the attribute name where value='true'
<xsl:for-each select="candy/#*[. = 'true']">
<xsl:attribute name="candytype">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xsl:text> </xsl:text>
<xsl:for-each>
OP's comment:
can I return each attribute name (not value) whose value='true'?
<xsl:for-each select="candy/#*[. = 'true']">
<xsl:value-of select="name()"/>
<xsl:text> </xsl:text>
<xsl:for-each>
In XSLT 2.0 simply evaluate this XPath (2.0) expression:
candy/#*[. = 'true']/concat(name(.), ' ')
What you want is
<xsl:for-each select="candy/#*">
Related
We have a GivenName field which comes as multiple tag in input. for ex:
<PersonName>
<Surname>BNWHBQQ</Surname>
<GivenName>Adam</GivenName>
<GivenName>Sam</GivenName>
<GivenName>Peter</GivenName>
</PersonName>
we need to concatenate all GivenName present in input and pass it in one tag, for ex:
<db:PR_OFFENDER>
<db:SURNAME>BNWHBQQ</db:SURNAME>
<db:GIVEN_NAME>Adam Sam Peter</db:GIVEN_NAME>
</db:PR_OFFENDER>
I tried:
I tried using for loop but thats of no use as i am getting multiple in output as well, something like:
<xsl:if test="out:PartyEntity/out:Person/out:PersonName/out:GivenName">
<xsl:for-each select="out:PartyEntity/out:Person/out:PersonName/out:GivenName">
< db:GIVEN_NAME>
<xsl:value-of select="normalize-space(.)"/>
</db:GIVEN_NAME>
</xsl:for-each>
</xsl:if>
I can use something like below, But the output does not look nice, and I can have multiple given names in input, so this format wont work either.
<xsl:value-of select="concat(out:PartyEntity/out:Person/out:PersonName/out:GivenName[1],' ',out:PartyEntity/out:Person/out:PersonName/out:GivenName[2], ' ')"/>
Thanks in Advance,
Vivek
Such a template shoud work properly:
<xsl:if test="out:GivenName">
<db:GIVEN_NAME>
<xsl:for-each select="out:GivenName">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</db:GIVEN_NAME>
</xsl:if>
You can see it working here: http://xsltransform.net/pNmBxZz/1
Hi all please i need a help. I just start to learn XSLT
I need get value from attribute, but name of the attribute is always different. I can get the name of attribute
The name of atribute is for example "TEST"
THIS WORK
<xsl:param name="thisNode" select="."/>
<xsl:value-of select="$thisNode/#TEST "/>
I need do something like this, But it doesnt work as i expected :/
<xsl:param name="thisNode" select="."/>
<xsl:variable name="AttrName" select="'TEST'" />
<xsl:value-of select="$thisNode/#$AttrName "/>
Is this way how to do it?? Thank U very much.
Try this...
<xsl:value-of select="$thisNode/#*[name()=$AttrName]"/>
#* will return all attributes, and then you apply the condition to only get one with the matching "name()".
As an aside, you don't need to use the thisNode variable here if you are just checking the current node. This should work too
<xsl:value-of select="#*[name()=$AttrName]"/>
I have
<xsl:for-each select="ancestor-or-self::*">
<xsl:variable name="expression" select="name()" ></xsl:variable>
</xsl:for-each>
below I have href and i want to set the value this expression variable in href #######
Delete
I also tried with :
Delete
None of them worked.
Can Anybody help me out how to do that?
Basically my task is to find the expression for current node and send the expression for the same in href?
Adding More Info :
<br/><xsl:for-each select="ancestor-or-self::*">
<xsl:variable name="expression" select="name()" />
</xsl:for-each>
<b>Click Me</b>
when above xsl code is transformed it is giving below error:
Variable or parameter 'expression' is undefined.
In XSLT1.0, you could try setting the variable like so:
<xsl:variable name="expression">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:variable name="name" select="name()"/>
<xsl:value-of select="$name"/>
</xsl:for-each>
</xsl:variable>
So, assuming the following XML structure
<As>
<a>
<Bs>
<b>5</b>
</Bs>
</a>
<a>
<Bs>
<b>9</b>
</Bs>
</a>
<a/>
<a>
<Bs>
<b>12</b>
<b>14</b>
<b>15</b>
</Bs>
</a>
</As>
If you were positioned on the b element with the value of 14, then expression would be set to /As/a/Bs/b
However, this does not take into account multiple nodes with the same name, and so would not be sufficient if you wanted accurate XPath to select the node.
Instead, you could try the following:
<xsl:variable name="expression">
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:variable name="name" select="name()"/>
<xsl:value-of select="$name"/>
<xsl:text>[</xsl:text>
<xsl:value-of select="count(preceding-sibling::*[name() = $name]) + 1"/>
<xsl:text>]</xsl:text>
</xsl:for-each>
</xsl:variable>
This would return /As[1]/a[4]/Bs[1]/b[2], which may be what you want.
Very simply, variables are scoped in XSL and exist only within the containing tag. Thus the variable named expression exists only within the for-each block. Also, variables can only be set once. Attempting to set a variable value a second time has no effect.
Therfore you have to declare the variable at or above the level where you want to use it, and put all the code to generate the value inside the variable declaration. If you can use XSLT2, the following will do what you want:
string-join(for $n in ancestor-or-self::* return name($n), '/')
I know it can also be done in XSLT 1 with a recursive template, but I don't have an example handy.
I have a variable in XSLT called variable_name which I am trying to set to 1, if the Product in question has attributes with name A or B or both A & B.
<xsl:variable name="variable_name">
<xsl:for-each select="product/attributes">
<xsl:if test="#attributename='A' or #attributename='B'">
<xsl:value-of select="1"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
Is there any way to match multiple strings using the if statement, as mine just matches if A is present or B is present. If both A & B are present, it does not set the variable to 1. Any help on this would be appreciated as I am a newbie in XSLT.
You can use xsl:choose statement, it's something like switch in common programming languages:
Example:
<xsl:variable name="variable_name">
<xsl:for-each select="product/attributes">
<xsl:choose>
<xsl:when test="#attributename='A'">
1
</xsl:when>
<xsl:when test=" #attributename='B'">
1
</xsl:when>
<!--... add other options here-->
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
This will set new variable with name variable_name with the value of attribute product/attributes.
For more info ... http://www.w3schools.comwww.w3schools.com/xsl/el_choose.asp
EDIT: And another way (a little dirty) by OP's request:
<xsl:variable name="variable_name">
<xsl:for-each select="product/attributes">
<xsl:if test="contains(text(), 'A') or contains(text(), 'B')">
1
</xsl:if>
</xsl:for-each>
</xsl:variable>
It will be helpful if you provide the xml you're writing your xslt against.
This might not help...
Is it 'legal' to have two XML element attributes with the same name (eg. <element x="1" x="2" />)?
Is this what you are trying to process? Try parsing your XML file through xmllint or something like it to see if it is valid.
xmllint --valid the-xml-file.xml
My guess is that you will get a 'attribute redefined' error.
Hi i have a strange issue with matching specific attribute of xml node.
Example code that doesnt work:
<xsl:for-each select="../../unit/service/price/season[#name=$period_name]">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="../#amount"/>
</xsl:attribute>
</xsl:for-each>
Example code that DOES work but i don't like this way too much:
<xsl:for-each select="../../unit/service/price/season">
<xsl:if test="#name = $period_name">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="../#amount"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
If in first example i replace the variable name with some of the values like 'A' it works,
i also tested what variable name is selected and it has the correct data inside (so, 'A','B','C' ...)
Anyone had this problem before?
Tnx
You might try changing it to an apply-templates instead of a foreach. Something like the following should work.
<xsl:template match="price">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="#amount" />
</xsl:attribute>
</xsl:template>
And then call it like:
<xsl:apply-template select="../../unit/service/price/[season/#name=$period_name]" />
Not seen this before. Could it be an ambiguous #name attribute. Therefore try accessing it like below?
select="../../unit/service/price/season[./#name=$period_name]
Other than that, sorry, to me it looks like it should work perfectly both ways.