Find child of child which attribute code is equal to the parameter passed on the url - XSL

白昼怎懂夜的黑 提交于 2020-01-05 09:28:29

问题


On this dynamic website, The url looks something like this : departments/CHEM.html

CHEM is a parameter.

     <xsl:param name="dep" select="'CHEM'" />

a piece of the xml is below

<course acad_year="2012" cat_num="5085" offered="Y">
  <term term_pattern_code="1" fall_term="Y" spring_term="N">fall term</term>
  <department code="CHEM">
     <dept_long_name>Department of Chemistry and Chemical Biology</dept_long_name>
     <dept_short_name>Chemistry and Chemical Biology</dept_short_name>
  </department>
</course> ....

I am trying to get the dept_short_name to use on my H1 tag, but I have not been successful.So far I tried

<h2><xsl:value-of select="course/department/[code={@$dep}]"/></h2>

Any suggestions??? Thanks!


回答1:


Just use:

<xsl:value-of select="course/department[@code eq $dep]/dept_short_name"/>

Remember:

In XPath 2.0 (XSLT 2.0) use the eq operator for value comparissons -- it is more efficient than the general comparisson operator = which really, only, needs to be used when at least one of its operands is a sequence.




回答2:


I would try this:

<xsl:value-of select="course/department[@code=$dep]/dept_short_name/text()"/>

That says: find the department element (inside a course element) whose code attribute is the value of parameter "dep", then find the dept_short_name child element, then get the text inside that element.

You have to use the @ to say that "code" is an attribute, but "dep" should not have it. I think the {} notation is for use inside attributes of the non-XSLT elements of your stylesheet, so I wouldn't use it inside a value-of expression.



来源:https://stackoverflow.com/questions/12877533/find-child-of-child-which-attribute-code-is-equal-to-the-parameter-passed-on-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!