None
None

Reputation: 5670

Select specific element from XML by XSLT

I have an XML like this in a variable prdxml

 <root>
  <product>
    <estocklevel>0</estocklevel>
    <id>8142229</id>
    <isp_brand extra="isp_brand"></isp_brand>
    <isp_produktserie extra="isp_produktserie"></isp_produktserie>
    <isp_model extra="isp_model"></isp_model>
  </product>
  <product>
    <estocklevel>0</estocklevel>
    <id>8143793</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Bred</isp_model>
  </product> 
  <product>
    <estocklevel>0</estocklevel>
    <id>8143794</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
  <product>
    <id>8143796</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
</root>

I want to select a product node where id =8143794 from this ,with out using for loop.can any one give any clue

Upvotes: 2

Views: 2253

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167401

Well with XSLT 1.0 we really need to know whether the type of the variable named prdxml is node-set or result tree fragment.

If it is a node-set you can simply select $prdxml/root/product[id = 8143794]. but if you have a result tree fragment you first need to apply an extension function like exsl:node-set e.g. exsl:node-set($prdxml)/root/product[id = 8143794].

So check where/how the variable is set, if you have e.g.

<xsl:variable name="prdxml" select="document('products.xml')"/>

you have a node-set, however with e.g.

<xsl:variable name="prdxml">
<root>
  <product>
    <estocklevel>0</estocklevel>
    <id>8142229</id>
    <isp_brand extra="isp_brand"></isp_brand>
    <isp_produktserie extra="isp_produktserie"></isp_produktserie>
    <isp_model extra="isp_model"></isp_model>
  </product>
  <product>
    <estocklevel>0</estocklevel>
    <id>8143793</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Bred</isp_model>
  </product> 
  <product>
    <estocklevel>0</estocklevel>
    <id>8143794</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
  <product>
    <id>8143796</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
</root>
</xsl:variable>

you have a result tree fragment and need the second approach (and support for exsl:node-setor similar):

<xsl:variable name="prod" select="exsl:node-set($prdxml)/root/product[id = 8143794]" xmlns:exsl="http://exslt.org/common"/>

Upvotes: 2

PaulProgrammer
PaulProgrammer

Reputation: 17620

/root/product[id='8143794']

That says "find /root/product where the "id" element child to "product" is 8143794"

There's an answer out there as to how to use the variable in a future query: XSL: How best to store a node in a variable and then us it in future xpath expressions?

Upvotes: 4

Related Questions