身份转换 - 比较属性和限制输出

时间:2016-01-26 11:04:49

标签: xslt xpath xslt-2.0 xpath-2.0

我想在同一步骤中比较两个xml-Files和identity的属性转换输入文件。输出xml应该只包含其属性出现在比较xml中的元素。如给出的示例所示,不应输出最后一个概念节点,因为comparison.xml中没有匹配属性

input.xml中

<navigation 
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<facets>
    <facet id="d1e12000000000000000000000011111">
        <title xml:lang="en">sometxt</title>
        <title xml:lang="de">eintxt</title>
        <concepts>
            <concept id="d1e12000000000000000000000000000">
                <title xml:lang="en">sometxt</title>
                <title xml:lang="de">eintxt</title>
                <concepts>
                    <concept id="d1e19000000000000000000000000000">
                        <title xml:lang="en">sometxt</title>
                        <title xml:lang="de">eintxt</title>
                        <concepts>
                        </concepts>
                    </concept>
                </concepts>
            </concept>
        </concepts>
    </facet>
</facets>

comparison.xml 的一部分与不确定的标题级别

<foo>
<heading class="d1e12000000000000000000000011111|d1e12000000000000000000000000000">Myheading</heading>
<chapter>
    <heading class="d1e12000000000000000000000011111|d1e12000000000000000000000000000">myheading</heading>
    <operation>
        <heading class="d1e12000000000000000000000011111|d1e12000000000000000000000000000">another heading</heading>
    </operation>
</chapter>

所需的 output.xml 仅包含适用的ID

<nav:navigation 
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nav="http://www.nav.de/">
<nav:facets>
    <nav:facet id="d1e12000000000000000000000011111">
        <nav:title xml:lang="en">sometxt</nav:title>
        <nav:title xml:lang="de">eintxt</nav:title>
        <nav:concepts>
            <nav:concept id="d1e12000000000000000000000000000">
                <nav:title xml:lang="en">sometxt</nav:title>
                <nav:title xml:lang="de">eintxt</nav:title>
                <nav:concepts>
                </nav:concepts>
            </nav:concept>
        </nav:concepts>
    </nav:facet>
</nav:facets>

到目前为止

我的 xsl

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:fo="http://www.w3.org/1999/XSL/Format"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:nav="http://www.nav.de/"
     version="2.0" >
        <xsl:output method="xml" indent="yes" encoding="utf-8"/>
        <xsl:variable name="docu" select="document(comparing.xml)"/>

        <xsl:template match="*">
            <xsl:element name="nav:{name()}" namespace="http://www.nav.de/">
                <xsl:copy-of select="namespace::*"/>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:element>
        </xsl:template>

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

编辑:很抱歉在评论部分发布此内容。我已尝试过这些方面的东西,但它没有工作

   <xsl:template match="concept | facet">
        <xsl:variable name="foo-id" select="@id"/>  
        <xsl:for-each select="$docu//heading">
            <xsl:if test="contains(./@class, $foo-id)">
                <xsl:apply-templates/>  
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

1 个答案:

答案 0 :(得分:1)

我建议你这样试试:

XSLT 2.0

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

<xsl:param name="comparing-url" select="'comparing.xml'"/>

<xsl:key name="comp" match="@class" use="tokenize(., '\|')" />

<xsl:template match="*">
    <xsl:element name="nav:{name()}" >
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="*[@id][not(key('comp', @id, document($comparing-url)))]"/>

</xsl:stylesheet>