Christian A
Christian A

Reputation: 501

xslt select attribute from variable

I'm with xslt, and have run into a problem(i'm new to this). I have some xml in a variable called gmlMember, and a variable containing a name called var_FeatureClassName which contains "BES_VANDLOEB"

The xml looks like this (contained in gmlMember):

<dmp:BES_VANDLOEB gml:id="BES_VANDLOEB.52626" xmlns:gml="http://www.opengis.net/gml" xmlns:dmp="https://arealinformation.miljoeportal.dk/gis/services/DAIdb/MapServer/WFSServer">
   <dmp:OBJECTID>94808</dmp:OBJECTID>
   <dmp:Temakode>2014</dmp:Temakode>
   <dmp:Temanavn>Beskyttede vandløb</dmp:Temanavn>
   <dmp:Objekt_id>{D2BDC519-5374-11E2-B629-00155D01E765}</dmp:Objekt_id>
   <dmp:Version_id>{F744FAB7-04A4-4DAE-8A03-A765D86CEA52}</dmp:Version_id>
   <dmp:Systid_fra>2006-12-31T23:59:00</dmp:Systid_fra>
   <dmp:Oprettet>2006-12-31T23:59:00</dmp:Oprettet>
</dmp:BES_VANDLOEB>

I need to get gml:id="BES_VANDLOEB.52626"

I have tried various things like the following:

  <xsl:variable name="gmlId">
    <xsl:value-of select="$gmlMember/$var_FeatureClassName/@gml:id"/>
  </xsl:variable>

That doesn't work. Does anyone know how I can get it?

Upvotes: 1

Views: 2871

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

A variable in XPath represents a value, not a fragment of an expression (it's not a macro language). So $a/$b means "for each value in $a return the value of $b". So if $a contains 6 nodes, and $b contains the string 'className', then the result will be a sequence containing 6 occurrences of the string 'className'. If you intended the result to be equivalent to the result of the expression $a/className, then you should have written $a/*[name()='className'] (or some refinement of that to take namespaces into account).

(This explanation applies to XSLT 2.0/3.0. In XSLT 1.0, writing $a/$b is a static error, it's not a valid expression. That's why I asked you to tell us how it failed.)

Upvotes: 3

Chertkov Pavel
Chertkov Pavel

Reputation: 43

I hope I understood correctly:

Here xslt example: https://xsltfiddle.liberty-development.net/gWcDMer/3

Xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gml="http://www.opengis.net/gml" xmlns:dmp="https://arealinformation.miljoeportal.dk/gis/services/DAIdb/MapServer/WFSServer">
    <xsl:template match="/">
        <xsl:variable name="gmlMember" select="."/>
        <xsl:variable name="var_FeatureClassName" select="'dmp:BES_VANDLOEB'" />
        <xsl:variable name="gmlId">
            <xsl:value-of select="$gmlMember//*[name()=$var_FeatureClassName]/@gml:id"/>
        </xsl:variable>

        <id>
            <xsl:value-of select="$gmlId"/>
        </id>
    </xsl:template>
</xsl:stylesheet>

Xml input:

<root>
    <dmp:BES_VANDLOEB gml:id="BES_VANDLOEB.52626" xmlns:gml="http://www.opengis.net/gml" xmlns:dmp="https://arealinformation.miljoeportal.dk/gis/services/DAIdb/MapServer/WFSServer">
        <dmp:OBJECTID>94808</dmp:OBJECTID>
        <dmp:Temakode>2014</dmp:Temakode>
        <dmp:Temanavn>Beskyttede vandløb</dmp:Temanavn>
        <dmp:Objekt_id>{D2BDC519-5374-11E2-B629-00155D01E765}</dmp:Objekt_id>
        <dmp:Version_id>{F744FAB7-04A4-4DAE-8A03-A765D86CEA52}</dmp:Version_id>
        <dmp:Systid_fra>2006-12-31T23:59:00</dmp:Systid_fra>
        <dmp:Oprettet>2006-12-31T23:59:00</dmp:Oprettet>
    </dmp:BES_VANDLOEB>
    <dmp:SOME_VANDLOEB gml:id="BES2126" xmlns:gml="http://www.opengis.net/gml" xmlns:dmp="https://arealinformation.miljoeportal.dk/gis/services/DAIdb/MapServer/WFSServer">
        <dmp:OBJECTID>94808</dmp:OBJECTID>
        <dmp:Temakode>2014</dmp:Temakode>
        <dmp:Temanavn>Beskyttede vandløb</dmp:Temanavn>
        <dmp:Objekt_id>{D2BDC519-5374-11E2-B629-00155D01E765}</dmp:Objekt_id>
        <dmp:Version_id>{F744FAB7-04A4-4DAE-8A03-A765D86CEA52}</dmp:Version_id>
        <dmp:Systid_fra>2006-12-31T23:59:00</dmp:Systid_fra>
        <dmp:Oprettet>2006-12-31T23:59:00</dmp:Oprettet>
    </dmp:SOME_VANDLOEB>
</root>

Output:

<id xmlns:gml="http://www.opengis.net/gml" xmlns:dmp="https://arealinformation.miljoeportal.dk/gis/services/DAIdb/MapServer/WFSServer">BES_VANDLOEB.52626</id>

Upvotes: 2

Related Questions