XSLT样式表?不知道怎么写?

时间:2011-12-14 00:15:13

标签: xslt

XML:

<tickets>
  <request day="tue">
    <ticket>
       <type>Tech Support</type>
       <cust>9045</cust>
       <status>pending</status>
    <ticket>
    <ticket>
       <type>Account Support</type>
       <cust>3344</cust>
       <status>resolved</status>
    <ticket>
  </request>
  <request day="wed">
    <ticket>
       <type>Tech Support</type>
       <cust>9944</cust>
       <status>new</status>
    <ticket>
    <ticket>
       <type>Billing</type>
       <cust>3344</cust>
       <status>pending</status>
    <ticket>
  </request>
</tickets>

如何编写select =“”以匹配所有cust元素值,不重复(如果有多于1个请求)?

2 个答案:

答案 0 :(得分:1)

  

如何编写select =“”来匹配所有cust元素值,   不重复(如果有多于1个请求)?

这是一个完整,简短且简单(无xsl:for-each且无长且无法读取的行)Muenchian grouping解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:key name="kCustByVal" match="cust" use="."/>

 <xsl:template match=
  "cust
     [generate-id()
     =
      generate-id(key('kCustByVal', .)[1])
     ]
  ">
  <xsl:value-of select="."/><xsl:text> </xsl:text>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

将此转换应用于提供的XML文档(经过多次更正后已经格式良好):

<tickets>
    <request day="tue">
        <ticket>
            <type>Tech Support</type>
            <cust>9045</cust>
            <status>pending</status>
        </ticket>
        <ticket>
            <type>Account Support</type>
            <cust>3344</cust>
            <status>resolved</status>
        </ticket>
    </request>
    <request day="wed">
        <ticket>
            <type>Tech Support</type>
            <cust>9944</cust>
            <status>new</status>
        </ticket>
        <ticket>
            <type>Billing</type>
            <cust>3344</cust>
            <status>pending</status>
        </ticket>
    </request>
</tickets>

产生了想要的正确结果

9045 3344 9944 

答案 1 :(得分:0)

这就是你要追求的吗?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8" indent="yes"/>
    <xsl:key name="cust" match="/tickets/request/ticket/cust/text()" use="."/>
    <xsl:template match="/">
        <xsl:for-each select="/tickets/request/ticket/cust/text()[generate-id()=generate-id(key('cust',.)[1])]">
            <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>