xslt-在带有特定text()

时间:2018-07-04 08:38:19

标签: xml xslt

我有以下XML文件,我应用XSLT文件输出一个文本文件,其中每个记录都定义了每一行。

<Data>
    <Record>
            <Identification1>AA123</Identification1>
            <SomeNode1>...</SomeNode1>
            <SomeNode2>...</SomeNode2>
            <SomeNode3>...</SomeNode3>
            <SecondIdent>XX123</SecondIdent>
            <SomeNode4>...</SomeNode4>
    </Record>
    <Record>
            <Identification1>BB123</Identification1>
            <SomeNode1>...</SomeNode1>
            <SomeNode2>...</SomeNode2>
            <SomeNode3>...</SomeNode3>
            <SecondIdent>XX123</SecondIdent>
            <SomeNode4>...</SomeNode4>
    </Record>
    <Record>
            <Identification1>CC124</Identification1>
            <SomeNode1>...</SomeNode1>
            <SomeNode2>...</SomeNode2>
            <SomeNode3>...</SomeNode3>
            <SecondIdent>AA123</SecondIdent>
            <SomeNode4>...</SomeNode4>
    </Record>
    <Record>
            <Identification1>DD123</Identification1>
            <SomeNode1>...</SomeNode1>
            <SomeNode2>...</SomeNode2>
            <SomeNode3>...</SomeNode3>
            <SecondIdent>XX123</SecondIdent>
            <SomeNode4>...</SomeNode4>
    </Record>
    <Record>
            <Identification1>DD123</Identification1>
            <SomeNode1>...</SomeNode1>
            <SomeNode2>...</SomeNode2>
            <SomeNode3>...</SomeNode3>
            <SecondIdent>XX123</SecondIdent>
            <SomeNode4>...</SomeNode4>
    </Record>
</Data>

我已经创建了一个代码(如下),该代码检查每个数据/记录并测试另一个节点(比如SomeNode2)是否具有8位以上的数字。效果很好。

输出为:

AA123;...;...;...;XX123;...
BB123;...;...;...;XX123;...
CC123;...;...;...;AA123;...
DD123;...;...;...;XX123;...
XX123;...;...;...;XX123;...

简单,容易,完成。

但是,有人要求排除文件的任何 SecondIdent 中具有 Identification1 的所有数据/记录,其特殊之处在于,该文件/记录应仅对那些以与“ AA”。这是最难解决的问题。

输出应如下所示:

BB123;...;...;...;XX123;...
CC123;...;...;...;AA123;...
DC123;...;...;...;XX123;...
XX123;...;...;...;XX123;...

我找不到合适的解决方案。

编辑:在此示例中, SecondIdent 中存在 Identification1 “ AA123”,应将其删除,因为它以'AA'开头。但是,存在 Identification1 “ XX123”,因为即使它多次出现也不以“ AA”开头。

我尝试了这些解决方案:

  • xsl:key:不知何故我无法正常工作。似乎比编码问题更多的是知识问题:)
  • 尝试创建一个变量“ $ count”,该变量用于计算Identification1在SecondIden中的出现次数。然后在下面的XLM中的if statemnt中,我添加了te变量“ = 0”(不起作用,空文档),并且直接在if语句中添加了计数

(count(/ Data / Record / EFA_ISIN_IDENT [contains(。,$ ISIN)])= 0

It worked but when I tried to make another condition based on "AA" then I ran into problems (data not displaying or he kicked out all Identification beginning with "AA").

xslt代码:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="UTF-8"/>
  <xsl:variable name="separator" select="';'"/>

<xsl:template match="/">

    <xsl:for-each select="Data/Record">
        <xsl:if test="( (string-length(format-number(./SomeNode2/text(),'0;0'))&lt;= '8') )">       
            <!--1.Identification-->
            <xsl:value-of select="./Identification1/text()"/>
            <xsl:value-of select="$separator"/>
            <!--2.SomeNode-->
            <xsl:value-of select="./SomeNode1/text()"/>
            <xsl:value-of select="$separator"/>
            <!--3.SomeNode-->
            <xsl:value-of select="./SomeNode2/text()"/>
            <xsl:value-of select="$separator"/> 
            <!--4.SecondIdent-->
            <xsl:value-of select="./SecondIdent/text()"/>
            <xsl:value-of select="$separator"/> 
            <!--5.SomeNode-->
            <xsl:value-of select="./SomeNode2/text()"/>
            <xsl:value-of select="$separator"/> 
            <xsl:value-of select="'&#xA;'" />
        </xsl:if>
    </xsl:for-each>

    </xsl:template> 

</xsl:stylesheet>   

最后但并非最不重要:对于我的英语以及所有错别字/语法,我深表歉意,谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您想要的表情是这个...

<xsl:for-each select="Data/Record[not(Identification1 = ../Record/SecondIdent and starts-with(Identification1, 'AA'))]">

如果要使用键(对于较大的数据集,可能会更好),请按以下方式定义键(此键必须是xsl:stylesheet的直接子代)

<xsl:key name="secondIdents" match="Record" use="SecondIdent" />

并像这样使用它。...

<xsl:for-each select="Data/Record[not(key('secondIdents', Identification1) and starts-with(Identification1, 'AA'))]">