XML/XSLT stylesheet/parent

Материал из Web эксперт
Перейти к: навигация, поиск

Don"t output text nodes unless explicitly told to

File: Data.xml
<wine grape="A">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1998</year>
  <desc>description</desc>
  <prices>
    <list>6.99</list>
    <discounted>5.99</discounted>
    <case>71.50</case>
  </prices>
</wine>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="text()" />
  <xsl:template match="list">
    ~~~~ Start of list element"s template ~~~~ 1. List price
    (current node): {
    <xsl:apply-templates />
    } 2. Parent element (prices) contents: {
    <xsl:value-of select=".." />
    } 3. Grandparent element contents: {
    <xsl:value-of select="../.." />
    } 4. Attribute of grandparent: {
    <xsl:value-of select="../../@grape" />
    } 5. Sibling node {
    <xsl:value-of select="../discounted" />
    } 6. Uncle node {
    <xsl:value-of select="../../product" />
    } 7. Parent node"s name: {
    <xsl:value-of select="name(..)" />
    } 8. Grandparent node"s name: {
    <xsl:value-of select="name(../..)" />
    } ~~~~ End of list element"s template ~~~~
  </xsl:template>
  
</xsl:stylesheet>
Output:

    ~~~~ Start of list element"s template ~~~~ 1. List price
    (current node): {
    
    } 2. Parent element (prices) contents: {
    
    6.99
    5.99
    71.50
  
    } 3. Grandparent element contents: {
    
  shop 1
  product 1
  1998
  description
  
    6.99
    5.99
    71.50
  
    } 4. Attribute of grandparent: {
    A
    } 5. Sibling node {
    5.99
    } 6. Uncle node {
    product 1
    } 7. Parent node"s name: {
    prices
    } 8. Grandparent node"s name: {
    wine
    } ~~~~ End of list element"s template ~~~~



Get sibling with ../

File: Data.xml
<vintage>
  <year>1998</year>
  <wine grape="B">
    <winery>A</winery>
  </wine>
</vintage>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="wine">
    <wine varietal="{@grape}" brand="{winery}" year="{../year}" />
  </xsl:template>
  <xsl:template match="year" />
  
</xsl:stylesheet>
Output:

  
  <wine varietal="B" brand="A" year="1998"/>



Select element out of parent tag

File: Data.xml
<employees>
  <employee hireDate="04/23/1999" officer="yes">
    <last>A</last>
    <first>B</first>
    <salary>100000</salary>
  </employee>
  <employee hireDate="09/01/1998" officer="no">
    <last>C</last>
    <first>D</first>
    <salary>95000</salary>
  </employee>
  <employee hireDate="08/20/2000">
    <last>E</last>
    <first>F</first>
    <salary>89000</salary>
  </employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="employee">
    <client>
      <xsl:apply-templates select="lastName" />
      <xsl:apply-templates select="phone" />
    </client>
  </xsl:template>
  
</xsl:stylesheet>
Output:

  <client/>
  <client/>
  <client/>



Use .. to indicate level

File: Data.xml
<winelist>
  <wine grape="A">
    <winery>shop 2</winery>
    <product>product 2</product>
    <year>2008</year>
    <prices>
      <list>10.99</list>
      <discounted>9.50</discounted>
    </prices>
  </wine>
</winelist>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  <xsl:template match="winelist">
    Wines needing their "discount" value set:
    <xsl:for-each select="wine/prices/discounted[not(text())]">
      <xsl:value-of select="../../year" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="../../winery" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="../../product" />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>