Matching by ID

Matching by ID

In addition to making patterns out of step patterns that specify an axis, node test, and predicate, you can also use the id() pattern to match elements that have a specific ID value. To use this pattern, you must give elements an ID attribute, and you must declare that attribute of type ID, as you can do in a DTD or schema. The following example rule adds the text of all elements that have the ID favorite:

 <xsl:template match = "id('favorite')">      <H3><xsl:value-of select="."/></H3>  </xsl:template> 

Heres what a DTD for planets.xml might look like that declares an ID and sets it to favorite:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="#stylesheet"?>  <!DOCTYPE PLANETS [ <!ELEMENT PLANET (CUSTOMER)*>  <!ELEMENT CUSTOMER (NAME,MASS,RADIUS,DAY)>  <!ELEMENT NAME (#PCDATA)>  <!ELEMENT MASS (#PCDATA)>  <!ELEMENT RADIUS (#PCDATA)>  <!ELEMENT DAY (#PCDATA)>  <!ATTLIST PLANET      id ID #REQUIRED>  ]>  <PLANETS>      <PLANET id="favorite">          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

Some XSLT processors dont match by ID because they dont read DDS or XML schema. (This is one of the issues that XSLT 2.0 is supposed to addresshow to make ID information available.) However, theres another option: you can match by key .

Possible Support for IDREFs

In addition to making it easier to work with IDs, W3C is even considering adding support for IDREFs in XSLT 2.0. In particular, given an ID, the XSLT processor could provide you with a list of all elements that have an IDREF or IDREFS attribute that refers to that ID. (Note that currently, however, you can do the same thing using <xsl:key> and the key() pattern.)

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: