2
votes

I'm sure I'm missing something simple here...

I can't select a nested xml element using an XLST transform.

Here is the xml

<collection>
<record>
  <leader>01814nam a2200205Ia 4500</leader>
  <controlfield tag="003">PSCA</controlfield>
  <controlfield tag="005">20141201150951.0</controlfield>
  <controlfield tag="008">131110s9999    xx            000 0 und d</controlfield>
  <datafield tag="040" ind1=" " ind2=" ">
    <subfield code="a">PSCA</subfield>
    <subfield code="c">Calyx</subfield>
  </datafield>
  <datafield tag="110" ind1=" " ind2=" ">
    <subfield code="9">76</subfield>
    <subfield code="a">Children's Services Central</subfield>
  </datafield>
  <datafield tag="245" ind1="0" ind2="0">
    <subfield code="a">What's Pedagogy Anyway?</subfield>
    <subfield code="b">Using pedagogical  documentation to engage  with the Early Years Learning  Framework </subfield>
  </datafield>
</record>
</collection

I need to select the following data:

/collection

/record

/datafield

-->/@tag='245'

---->/subfield/@code='a'

I would expect the output to be: "What's Pedagogy Anyway?"

My XLST to select this node/data is as such:

<xsl:for-each select="collection/record/datafield">
    <xsl:choose>
        <xsl:when test="@tag=245">
            <xsl:choose>
                <xsl:when test="/subfeild/@code=a">
                    <xsl:value-of select="/subfeild"/>
                </xsl:when> 
            </xsl:choose>
        </xsl:when>
    </xsl:choose>
 </xsl:for-each>

I'm running this using the lxml library in Python if that's important. The code compiles and runs without error. My output is null.

Any ideas? Thanks in advance.

2

2 Answers

1
votes

You might want to change @code=a with @code='a', such as:

 <xsl:template match="/">
    <xsl:value-of select="collection/record/datafield[@tag=245]/subfield[@code='a']"/>
</xsl:template>

EDIT

If you insist on having a for-each loop, see below:

<xsl:template match="/">
    <xsl:for-each select="collection/record/datafield">
        <xsl:choose>
            <xsl:when test="@tag=245">
                <xsl:value-of select="subfield[@code='a']"/>
            </xsl:when>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>
0
votes

This is what I needed

<xsl:value-of select="collection/record/datafield[@tag=245]/subfield[@code='a']" />
<xsl:for-each select="collection/record">
    <xsl:value-of select="datafield[@tag=245]/subfield[@code='a']"/>
</xsl:for-each>

I should explain - I need to loop as the XML provided is a fragment of well over 500 complex records. Not this one instance.