I am new to this forum. I need to parse/read an xml which cotains lots of metadata(as below). I have two read a specific meta value based on the bu value, for example if my bu value is '987654321abcdefghijklmnopaslasjdoiwneois123abc' I have to get the value "d581452fa9ba3110VgnVCM10000038ccc5abRCRD|def". My question, is there any restriction with metadata name length? for some reason my xslt logic is not reading this value(select="MT[#N=$partialval]/#V").
XSLT snippet:
<xsl:for-each select='./MT[#N="bu"]'>
<xsl:variable name="temp_bu" select="./#V"/>
<xsl:variable name="partialval" select="'987654321abcdefghijklmnopaslasjdoiwneois123abc'"/>
<xsl:if test="contains($partialval, $temp_bu)">
<xsl:variable name="partialbuval" select="MT[#N=$partialval]/#V"/>
<xsl:value-of disable-output-escaping='yes' select="concat('file://',$partialbuval,'test',$partialval)"/>
</xsl:if>
</xsl:for-each>
Sample xml structure:
<MT N="bu" V="12345678910111121314sdnfske34234123234abc"/>
<MT N="12345678910111121314sdnfske34234123234abc" V="02715148a3e7a110VgnVCM1000001b89c5abRCRD|abc"/>
<MT N="12345678910111121314sdnfske34234123234abc_name" V="Item one"/>
<MT N="bu" V="987654321abcdefghijklmnopaslasjdoiwneois123abc"/>
<MT N="987654321abcdefghijklmnopaslasjdoiwneois123abc" V="d581452fa9ba3110VgnVCM10000038ccc5abRCRD|def"/>
<MT N="987654321abcdefghijklmnopaslasjdoiwneois123abc_name" V="Item two"/>
<MT N="bu" V="qqqqqwwwweeeee1112223333ssdfff3334sssssabc"/>
<MT N="qqqqqwwwweeeee1112223333ssdfff3334sssssabc" V="d581452fa9ba3110VgnVCM10000038ccc5abRCRD|ghi"/>
<MT N="qqqqqwwwweeeee1112223333ssdfff3334sssssabc_name" V="Item three"/>
<MT N="bu" V="hhhheeeertisodfnsdifwoein12345sfjsoif234defgh"/>
<MT N="hhhheeeertisodfnsdifwoein12345sfjsoif234defgh" V="d581452fa9ba3110VgnVCM10000038ccc5abRCRD|jkl"/>
<MT N="hhhheeeertisodfnsdifwoein12345sfjsoif234defgh_name" V="Item four"/>
Seeing the output as (partialbuval value is missing in this string)
"file://test987654321abcdefghijklmnopaslasjdoiwneois123abc"
I am not sure what mistake I am doing here. Appreciate if you could help me.
Thanks,
Amarnath
I am not sure I understand your requirement. But in the following:
<xsl:for-each select="./MT[#N="bu"]">
<xsl:variable ... select="MT[#N=$partialval]/#V"/>
</xsl:for-each>
the second select will never select anything. That is because xsl:for-each changes the context. So within the body of the xsl:for-each (also called its sequence constructor) the context item is the current MT element being looked at in this current iteration of the xsl:for-each.
So the second select expression asks for any MT element, direct child of the current MT element, and there is not any.
Extra tip: ./ at the beginning of an XPath expression is always meaningless. The only situation where starting an expression with a dot is .//. So ./MT is always equivalent to MT, which is also more readable.
Related
I have the following XML structure:
<itemsWithLabels>
<itemLabelValue>
<label>A</label>
<value>a</value>
</itemLabelValue>
<itemLabelValue>
<label>B</label>
<value>b</value>
</itemLabelValue>
<itemLabelValue>
<label>C</label>
<value>c</value>
</itemLabelValue>
</itemsWithLabels>
Using XSL I want to be able to get the value from <value> by knowing the label in <label>.
So my transformation looks like this:
<xsl:value-of select="$content/itemsWithLabels/itemLabelValue/value[#label='A']" />
But clearly something is wrong because I don't have any output.
What am I doing wrong?
Try:
<xsl:value-of select="$content/itemsWithLabels/itemLabelValue[label='A']/value" />
$content/itemsWithLabels/itemLabelValue[label='A'] gets itemLabelValue elements with a label element child having value A. The /value part gets the child value element.
xsl
<xsl:variable name="varName>
<xsl:value-of select="/can/be/a/long/path/down/xml/item[#id=1] />
</xsl:variable>
xml
<xml>
<item id="1" text="Yes">
<item id="2" text="No">
</xml>
use
I was thinking I could use like this:
<xsl:when test="$varName/#text = 'Yes'">
blah
</xsl:when>
but blank space is generated in place of variable. Is this even possible, have a node as a variable and use elsewhere?
<xsl:variable name="varName">
<xsl:value-of select="/can/be/a/long/path/down/xml/item[#id=1]" />
</xsl:variable>
This is one of the most common XSLT errors I see. Usually what people intended is:
<xsl:variable name="varName" select="/can/be/a/long/path/down/xml/item[#id=1]"/>
And most of the time, the code works just fine, except that it's a lot slower than it needs to be. But sometimes the fact that the two constructs are quite different beneath the covers comes back to bite you.
To understand the difference, xsl:variable with a select attribute binds the variable to whatever the select expression evaluates to, which in this case is a set of zero or more item elements. By contrast, xsl:variable with nested instructions creates a document node (XSLT 2.0) or result tree fragment (XSLT 1.0) whose content is a COPY of whatever those instructions produce. In this case, because the content is an xsl:value-of instruction, the variable contains a copy of the string-value of the selected node.
And of course, the string value of the selected node doesn't have any attributes, so test="$varname/#text = 'x'" will always return false.
I'm trying to reformat an XML I get from an appliance into an HTML table, and it's format is not usual.
It use unique references in node name's, like this:
/network/content/host/content/REF_1/content
/network/content/network/content/REF_2/content
and then, it use the same references to another part of the file, as a value of a content node, like this:
/rules/content/rules/content/REF_3/content/sources/content/name = REF_1
/rules/content/rules/content/REF_3/content/destinations/content/name = REF_2
I'm trying to write a template for content that instead of getting me REF_ID which is unique, I try to get the name, in the other branch leaf. this mean I'm trying to find a value that is out of my actual context.
I'm able to retrieve the name XPATH using this variable:
<xsl:variable name='objName' select="concat('/storage/objects/',#linkclass,'/content/',#linktype,'/content/',current(),'/content/name/content')" />
but, I'm not able to use this XPATH in a query like:
<xsl:value-of select="{$objName}">
I suppose this doesn't work because it's out of context but when I ask statically for one of those XPATH I get the value.
My full code is not very complicated:
<xsl:template match="content">
<xsl:variable name='objXPATH' select="concat('/storage/objects/',#linkclass,'/content/',#linktype,'/content/',current(),'/content/name/content')" />
<xsl:variable name='obj' select="{$objXPATH}" />
<xsl:element name="a">
<xsl:attribute name="href">
#<xsl:value-of select="."/>
</xsl:attribute>
<xsl:value-of select="$obj"/>
<br />
</xsl:element>
</xsl:template>
I need help to fix this, I'm on it since one day with no evolution, and it's driving me crazy. I'm more like a script kiddie than a real developer.
Dynamic evaluation (treating a string in a variable as an XPath expression and evaluating it) is available as a vendor extension in a number of XSLT processors, and it becomes part of the standard with the introduction of xsl:evaluate in XSLT 3.0. If your XSLT processor doesn't have such an extension you may be able to write it yourself. Alternatively, if you explain the problem better, we may be able to suggest a solution that does not require dynamic evaluation.
I have an XML with 2 XML fragments, 1st one is a fragment where the new values must be applied (which can have pretty complex elements) like
... some static parents
<a:element1>
<a:subelement tag="someString">
<a:s1>a</a:s1>
</a:subelement>
</a:element1>
<a:element2>b</a:element2>
<a:element3>c</a:element3>
... lots of other elements like the above ones
and 2nd fragment that has XPaths generated from the first XML and a new value, like
<field>
<xpath>/Parent/element1/subelement[#tag="someString"]/s1</xpath>
<newValue>1</newValue>
</field>
<field>
<xpath>/Parent/element2</xpath>
<newValue>2</newValue>
</field>
We might not have new values to apply for all the elements in the first fragment.
I'm struggling to make an XSLT transformation that should apply the new values to the places indicated by the XPaths.
The output should be:
... some static parents
<a:element1>
<a:subelement tag="someString">
<a:s1>1</a:s1>
</a:subelement>
</a:element1>
<a:element2>2</a:element2>
... lots of other elements like the above ones
I have access to xalan:evaluate to evaluate the dynamic xpath. I'm trying different solutions, I will write them here when they will start to make sense.
Any ideas of approaches are well received. Thanks
Oki, I found out how, and I will write the answer here maybe someone sometime will need this:
<xsl:template match="/">
<!-- static parents -->
<a:Root>
<xsl:apply-templates select="/a:Root/a:Parent" />
</a:Root>
</xsl:template>
<xsl:template match="#*|*|text()">
<xsl:variable name="x" select="generate-id(../.)" />
<xsl:variable name="y" select="//field[generate-id(xalan:evaluate(xpath)) = $x]" />
<xsl:choose>
<xsl:when test="$y">
<xsl:value-of select="$y/newValue" />
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="#*|*|text()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And to explain the transformation:
I'm writing down part that is static and then call apply-templates on the fragment I'm interested in, that has a liquid structure.
Then I'm using a slightly modified identity transformation that copies everything from source to target (starting from the /a:Root/a:Parent fragment), except when we position ourselves on the text I'm interested in changing.
The text() I'm interested in will have as parent (../.) the element referred by an xpath string found in the second fragment. Variable x means, in the context of the when, this element.
Variable y finds a field element that has as child an xpath element that if evaluated using xalan will refer to the same element that the x variable relates to.
Now I used generate-id() in order to compare the physical elements, otherwise it would have compared by the toString of the element (which is wrong). If variable y doesn't exist, it means that I have no xpath element for this element that could have changed, and I'm leaving it alone. If the y variable exists, I can get from it the newValue and I'm currently positioned on the element which text I want to update.
I have a variable from which I need to dynamically generate nodes
<xsl:template match="banner_discount_1 | banner_discount_2 | banner_discount_3">
<xsl:variable name="link">banner_discount_<xsl:value-of select="substring-after(name(.) ,'banner_discount_')" />_link</xsl:variable>
<xsl:value-of select="$link" />
</xsl:template>
<xsl:value-of> selects the string, but I want to be able to select the node which name matches the name of a variable.
In my case the node looks something like this:
<banner_discount_1_link />
<banner_discount_2_link />
...
Here is the xml I'm using
<banner_discount_1> 12 </banner_discount_1>
<banner_discount_2> 21 </banner_discount_2>
<banner_discount_3> 32 </banner_discount_3>
<banner_discount_1_link> link1 </banner_discount_1_link>
<banner_discount_2_link> link2 </banner_discount_2_link>
<banner_discount_3_link> link3 </banner_discount_3_link>
#MartinHonnen is on the right track, but you need to set the selection context as well.
Since you're in a template that's selecting the banner_discount_ nodes, that is your context. From your XML sample, it looks like the nodes you want to select are siblings, so this should work:
<xsl:value-of select="../*[local-name() = $link]"/>
It is preferable to target the nodes directly, but if they could be anywhere in the document, then you may resort to
<xsl:value-of select="//*[local-name() = $link]"/>
This is a last resort because it is potentially O(n) with respect to the number of nodes in the document.
Use <xsl:value-of select="*[local-name() = $link]"/>. If that does not help then consider to show a sample of the XML.