多个匹配条件检入xslt

时间:2016-02-16 07:26:50

标签: xml xslt

我有一个像这样的xml:

<root>
    <p>Hellooooo world</p>
    <drawing>
        <blip embed="rId11"/>
    </drawing>
    <p>This is testing</p>
    <drawing>
      <blip embed="rId12"/>
    </drawing>
    <p>This is testing2</p>
    <drawing>
        <blip embed="rId13"/>
    </drawing>
    <p>This is testing3</p>
    <part name="/word/media/image3.png" contentType="image/png" compression="store">
        <binaryData>Test1</binaryData>
    </part>
    <part name="/word/media/image4.png" contentType="image/png" compression="store">
        <binaryData>Test2</binaryData>
    </part>
    <part name="/word/media/image5.png" contentType="image/png" compression="store">
        <binaryData>Test3</binaryData>
    </part>
    <Relationship Id="rId11" Target="media/image3.png" />
    <Relationship Id="rId12" Target="media/image4.png" />
    <Relationship Id="rId13" Target="media/image5.png" />
</root>

我的相应XSL文件如下所示:

<xsl:template match="drawing">
        <xsl:variable name="ww" select="./generate-id()"/>
        <xsl:variable name="field_id" select="//blip/@embed"/>
        <xsl:variable name="reference" select="//Relationship[@Id = $field_id]"/>
        <xsl:variable name="destination" select="//part[replace(@name,'/word/','') = $reference/@Target]"/>
        <xsl:apply-templates select="$destination/binaryData"/>
    </xsl:template>

我得到的输出为:

<root>   
<p>Hellooooo world</p>   
<binaryData>Test1</binaryData>
<binaryData>Test2</binaryData>
<binaryData>Test3</binaryData>
<p>This is testing</p>   
<binaryData>Test1</binaryData>
<binaryData>Test2</binaryData>
<binaryData>Test3</binaryData> 
<p>This is testing2</p>
<binaryData>Test1</binaryData>
<binaryData>Test2</binaryData>
<binaryData>Test3</binaryData>
<p>This is testing3</p>
</root>

使用上面的代码转换后,我得到重复的内容,我希望类似的内容应该来了。 我的预期输出是:

<root>
<p>Hellooooo world</p>
<drawing>
<binarydata>Test1</binarydata>
</drawing>
<p>This is testing</p>
<drawing>
<binarydata>Test2</binarydata>
</drawing>
<p>This is testing2</p>
<drawing>
<binarydata>Test3</binarydata>
</drawing>
<p>This is testing3</p>
</root>

任何人都可以帮助我..

1 个答案:

答案 0 :(得分:1)

变化

<xsl:variable name="field_id" select="//blip/@embed"/>

<xsl:variable name="field_id" select="blip/@embed"/>