Page 1 of 1

Adress text of child code

Posted: Thu Aug 16, 2018 4:30 pm
by poroton
Hi,

I apply following template and want foo get filled with "bar1" or "bar2". How can I select the value of abc:foo?

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:abc="http://example.net/V3.0/ABCSchema" exclude-result-prefixes="abc">
<xsl:template match="/">
<xsl:apply-templates select="//abc:test/abc:vertreter"/>
</xsl:template>
<xsl:template match="//abc:test/abc:vertreter">
<Vertreter>
<xsl:attribute name="foo"><xsl:value-of select="node()/foo/text()"/></xsl:attribute>
<xsl:attribute name="ID"><xsl:value-of select="position()"/></xsl:attribute>
</Vertreter>
</xsl:template>

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<abc:test xmlns:abc="http://example.net/V3.0/ABCSchema">
<abc:vertreter>
<abc:foo>bar1</abc:foo>

</abc:vertreter>
<abc:vertreter>
<abc:foo>bar2</abc:foo>

</abc:vertreter>
</abc:test>
Thanks for your help!

Re: Adress text of child code

Posted: Fri Aug 17, 2018 7:43 am
by Radu
Hi,

Maybe replace:

Code: Select all

<xsl:value-of select="node()/foo/text()"/>
with:

Code: Select all


<xsl:value-of select="abc:foo"/>
Explanation:

1) The "node()" path means any child of the current element. As the current element is "vertreter", an expression like "node()/foo" would mean search for a grandchild called "foo" but "foo" is a child of "vertreter".

2) As all the elements in the XML are namespaced, you need to use the "abc:" prefix whenever referencing element names from the XML in the XPath.

3) Two expressions like this:

Code: Select all

<xsl:value-of select="abc:foo"/>
and:

Code: Select all

<xsl:value-of select="abc:foo/text()"/>
are equivalent because "xsl:value-of" basically means "get the text content of".

Regards,
Radu