要应用于所需元素的xsl模板

时间:2016-07-29 06:18:55

标签: xslt

此查询的答案很少,但无法实现我想要的目标。



<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<Data>
<Lookup Name="InterchangeControlVersion">2</Lookup>
<Lookup Name="InterchangeReceiverID">whsmith</Lookup>
<Lookup Name="InterchangeReceiverQual">zzz</Lookup>
<Lookup Name="InterchangeSenderID">blackstone</Lookup>
<Lookup Name="InterchangeSenderQual">zz</Lookup>
<Lookup Name="Standard">bookexchange</Lookup>
<Properties Name="TPName1">abc</Properties>
<Properties Name="TPName2">ABC</Properties>
<Properties Name="TPName3">ApPlE</Properties>
</Data>

<book category="cooking">
  <extrainfo>
    <details1>best</details1>
    <details2>cakebake</details2>
  </extrainfo>
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="web">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

<publisher>
  <name>McBeth</name>
  <add1>Per Bothner</add1>
  <add2>paris</add2>
  <company>
    <vat>no</vat>
    <reg>SSaf1123</reg>
  </company>
</publisher>

<publisher-group1>
  <name>McBeth</name>
  <add1>Per Bothner</add1>
  <add2>paris</add2>
  <company>
    <vat>no</vat>
    <reg>SSaf1123</reg>
  </company>
</publisher-group1>

</bookstore>
&#13;
&#13;
&#13;

我想仅将元素值转换为大写,我不想在数据段上进行任何转换。 我想要我的输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
   <Data>
      <Lookup Name="InterchangeControlVersion">2</Lookup>
      <Lookup Name="InterchangeReceiverID">whsmith</Lookup>
      <Lookup Name="InterchangeReceiverQual">zzz</Lookup>
      <Lookup Name="InterchangeSenderID">blackstone</Lookup>
      <Lookup Name="InterchangeSenderQual">zz</Lookup>
      <Lookup Name="Standard">bookexchange</Lookup>
   </Data>
   <book category="cooking">
      <title lang="en">EVERYDAY ITALIAN</title>
      <author>GIADA DE LAURENTIIS</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   <book category="children">
      <title lang="en">HARRY POTTER</title>
      <author>J K. ROWLING</author>
      <year>2005</year>
      <price>29.99</price>
   </book>
   <book category="web">
      <title lang="en">XQUERY KICK START</title>
      <author>JAMES MCGOVERN</author>
      <author>PER BOTHNER</author>
      <author>KURT CAGLE</author>
      <author>JAMES LINN</author>
      <author>VAIDYANATHAN NAGARAJAN</author>
      <year>2003</year>
      <price>49.99</price>
   </book>
   <book category="web">
      <title lang="en">LEARNING XML</title>
      <author>ERIK T. RAY</author>
      <year>2003</year>
      <price>39.95</price>
   </book>
</bookstore>

我目前正在使用下面的xsl,但这是为每个值进行转换。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

    <!-- Match document -->
    <xsl:template match="/">
        <xsl:apply-templates mode="copy" select="."/>
    </xsl:template>
    <!-- Deep copy template -->
    <xsl:template match="*|text()|@*" mode="copy">
        <xsl:copy>
            <xsl:apply-templates mode="copy" select="@*"/>
            <xsl:apply-templates mode="copy"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*|text()|@*" mode="copy">
        <xsl:message>123</xsl:message>
        <xsl:apply-templates mode="uppercase" select="."/>
    </xsl:template>

    <!-- Deep copy uppercase -->
    <xsl:template match="*|@*" mode="uppercase">
        <xsl:copy>
            <xsl:apply-templates mode="uppercase" select="@*"/>
            <xsl:apply-templates mode="uppercase"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()" mode="uppercase">
        <xsl:value-of select="translate(., $smallCase, $upperCase)"/>
    </xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

你为什么不这样做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="title | author">
    <xsl:copy>
        <xsl:value-of select="translate(., $lowerCase, $upperCase)"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

加了:

  

我想将我的大写模板应用于Book和if等所有细分   除了“数据”

之外还有更多

然后做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()[not(ancestor::Data)]">
    <xsl:value-of select="translate(., $lowerCase, $upperCase)"/>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

您的解决方案不需要那么复杂。您只需要身份模板加上一个匹配的book/*/text(),如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <xsl:variable name="smallCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

    <xsl:template match="book/*/text()"><xsl:value-of select="translate(., $smallCase, $upperCase)"/></xsl:template>

</xsl:stylesheet>