用于转换XML文件的XSLT样式表查询

时间:2014-04-03 07:45:36

标签: xml xslt xslt-1.0

我是xsl的新手。   我需要编写一个xslt代码来转换下面的xml代码         如果我有一个' LongCallService'标签超过2然后         我需要选择这个First' LongCallService'标记和检查天气它包含' inviceCode'子子元素标签里面' chargeGroup'或不 !!如果是,则需要显示。请帮忙。

XML代码:

  <RelatedCharges>
<CVOIP>
//other child element</<CVOIP>>
<Internet>
//other child element</Internet>

<LongCallService>
    <chargeGroup>
    <InvoiceCode>some number</InvoiceCode>
    </chargeGroup>      
</LongCallService>

<LongCallService>
//other child element(child Structure same as above first sibling)</LongCallService>
<LongCallService>
//other child element(child Structure same as above first sibling)</LongCallService>
</RelatedCharges>

1 个答案:

答案 0 :(得分:0)

根据您的描述,我创建了一个XML文件,如:

<?xml version="1.0" encoding="UTF-8"?>
<RelatedCharges>
    <CVOIP> //other child element</CVOIP>
    <Internet>//other child element</Internet>

    <LongCallService>
        <chargeGroup>
            <Code></Code>
        </chargeGroup>
        <InvoiceCode>some number</InvoiceCode>
    </LongCallService>

    <LongCallService>//other child element</LongCallService>

    <LongCallService>//other child element</LongCallService>
</RelatedCharges>

应用此样式表时:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:if test="count(RelatedCharges/LongCallService) &gt; 2">
            <xsl:copy-of select="RelatedCharges/LongCallService[1][chargeGroup/InvoiceCode]"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

输出,但如果第一个<LongCallService>如下:

    <LongCallService>
        <chargeGroup>
            <Code></Code>
        <InvoiceCode>some number</InvoiceCode>
        </chargeGroup>
    </LongCallService>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<LongCallService>
   <chargeGroup>
      <Code/>
      <InvoiceCode>some number</InvoiceCode>
   </chargeGroup>
</LongCallService>