【问题标题】:XSLT 1.0 - Creating nodes based on attribute values from different nodesXSLT 1.0 - 基于来自不同节点的属性值创建节点
【发布时间】:2020-11-13 12:11:42
【问题描述】:

我很难处理以下用例。

这是 XML:

<NodeA>
  <NodeB>
    <Application id="I-555" name="Text1" XorY="Y" />
    <Application id="I-666" name="Text2" XorY="X" />
    <Application id="I-777" name="Text3" XorY="Y" />
    <Application id="I-888" name="Text4" XorY="X" />
  </NodeB>
 </NodeA>

  <NodeD>
   <NodeE>
     <Process id="111" name="Text1" />
     <Process id="222" name="Text2" />
     <Process id="333" name="Text2" />
     <Process id="444" name="Text2" />
   </NodeE>
 </NodeD>

  <Links_between_Process_and_Application>
     <Link_Process_App app_id="I-555" process_id="111" />
     <Link_Process_App app_id="I-666" process_id="222" />
     <Link_Process_App app_id="I-777" process_id="333" />
     <Link_Process_App app_id="I-888" process_id="444" />
 </Links_between_Process_and_Application>

我想要实现的是基于节点 &lt;Links_between_Process_and_Application&gt; 在节点“应用程序”和“进程”之间创建/转换具有两个新链接的新 XML。

棘手的部分是(至少对我而言)是根据XorY(伪代码)的属性值创建两个不同的链接/节点:

通过&lt;Links_between_Process_and_Application&gt; 遍历(for-each)时 IF(检查当前的 app_id(例如 I-555)如果 @XorY='X' 在节点“应用程序”中) 创建 Link_Process_Application> ELSE IF (检查当前的 app_id (例如 I-555) if @XorY='Y' in node "Application") 创建 Link_Process_IDP>

我为转换编写了两个 XSLT 并生成了这两个链接(没有检查 XorY 属性),因为我真的不知道如何检查这个用例的不同属性的值:

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
        <ImportSchemaBase>
            <xsl:for-each select="Links_between_Process_and_Application/Link_Process_App">                  
                    <Link_Process_Application>
                        <SourceKey>
                            <xsl:value-of select="@process_id"/>
                        </SourceKey>
                        <TargetKey>
                            <xsl:value-of select="@app_id"/>
                        </TargetKey>
                    </Link_Process_Application>
            </xsl:for-each>
        </ImportSchemaBase>
    </xsl:template>
</xsl:stylesheet>

和:

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="/">
       <ImportSchemaBase>
            <xsl:for-each select="Links_between_Process_and_Application/Link_Process_App">                  
                    <Link_Process_IDP>
                        <SourceKey>
                            <xsl:value-of select="@process_id"/>
                        </SourceKey>
                        <TargetKey>
                            <xsl:value-of select="@app_id"/>
                        </TargetKey>
                    </Link_Process_IDP>  
            </xsl:for-each>
        </ImportSchemaBase>
    </xsl:template>
</xsl:stylesheet>

结果应该是:

创建 Link_Process_IDP,因为对于“app_id=I-555”,XorY 是 Y

<Link_Process_IDP>
   <SourceKey>111</SourceKey>
   <TargetKey>I-555</TargetKey>
</Link_Process_IDP>

创建 Link_Process_Application 因为对于 "app_id=I-666" XorY 是 X

<Link_Process_Application>
   <SourceKey>222</SourceKey>
    <TargetKey>I-666</TargetKey>
</Link_Process_Application>

创建 Link_Process_IDP,因为对于“app_id=I-777”,XorY 是 Y

<Link_Process_IDP>
   <SourceKey>333</SourceKey>
    <TargetKey>I-777</TargetKey>
</Link_Process_IDP>

创建 Link_Process_Application 因为对于 "app_id=I-888" XorY 是 X

<Link_Process_Application>
   <SourceKey>444</SourceKey>
    <TargetKey>I-888</TargetKey>
</Link_Process_Application>

非常感谢,对冗长的描述感到抱歉!

【问题讨论】:

  • 哪里有 ELSEIF,你也应该有 ELSE。否则这两种状态是互斥的,不需要测试Y

标签: xml xslt xslt-1.0


【解决方案1】:

您的输入不是 XML - 它缺少单个根元素(您的预期输出也是如此)。

给定格式良好的 XML 输入,使用 key 很容易完成任务:

XML

<root> 
    <NodeA>
        <NodeB>
            <Application id="I-555" name="Text1" XorY="Y" />
            <Application id="I-666" name="Text2" XorY="X" />
            <Application id="I-777" name="Text3" XorY="Y" />
            <Application id="I-888" name="Text4" XorY="X" />
        </NodeB>
    </NodeA>
    <NodeD>
         <NodeE>
             <Process id="111" name="Text1" />
             <Process id="222" name="Text2" />
             <Process id="333" name="Text2" />
             <Process id="444" name="Text2" />
         </NodeE>
    </NodeD>
    <Links_between_Process_and_Application>
         <Link_Process_App app_id="I-555" process_id="111" />
         <Link_Process_App app_id="I-666" process_id="222" />
         <Link_Process_App app_id="I-777" process_id="333" />
         <Link_Process_App app_id="I-888" process_id="444" />
     </Links_between_Process_and_Application>
</root>

XSLT 1.0

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

<xsl:key name="app" match="Application" use="@id" />

<xsl:template match="/root">
    <output>
        <xsl:for-each select="Links_between_Process_and_Application/Link_Process_App">
            <xsl:variable name="xory" select="key('app', @app_id)/@XorY" />
            <xsl:variable name="name">
                <xsl:choose>
                    <xsl:when test="$xory = 'X'">Link_Process_Application</xsl:when>
                    <xsl:when test="$xory = 'Y'">Link_Process_IDP</xsl:when>
                    <xsl:otherwise>SomethingElse</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:element name="{$name}">
                <SourceKey>
                    <xsl:value-of select="@process_id"/>
                </SourceKey>
                <TargetKey>
                    <xsl:value-of select="@app_id"/>
                </TargetKey>
            </xsl:element>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <Link_Process_IDP>
    <SourceKey>111</SourceKey>
    <TargetKey>I-555</TargetKey>
  </Link_Process_IDP>
  <Link_Process_Application>
    <SourceKey>222</SourceKey>
    <TargetKey>I-666</TargetKey>
  </Link_Process_Application>
  <Link_Process_IDP>
    <SourceKey>333</SourceKey>
    <TargetKey>I-777</TargetKey>
  </Link_Process_IDP>
  <Link_Process_Application>
    <SourceKey>444</SourceKey>
    <TargetKey>I-888</TargetKey>
  </Link_Process_Application>
</output>

请注意,如果 XY 是互斥的,您可以将其进一步缩短为:

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

<xsl:key name="app" match="Application" use="@id" />

<xsl:template match="/root">
    <output>
        <xsl:for-each select="Links_between_Process_and_Application/Link_Process_App">
            <xsl:variable name="name">
                <xsl:choose>
                    <xsl:when test="key('app', @app_id)/@XorY = 'X'">Link_Process_Application</xsl:when>
                    <xsl:otherwise>Link_Process_IDP</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:element name="{$name}">
                <SourceKey>
                    <xsl:value-of select="@process_id"/>
                </SourceKey>
                <TargetKey>
                    <xsl:value-of select="@app_id"/>
                </TargetKey>
            </xsl:element>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    我会这样做:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
        
      <xsl:key name="app" match="Application" use="@id"/>
    
      <xsl:output method="xml" indent="yes"/>
      
      <xsl:template match="/">
          <xsl:apply-templates select="//Link_Process_App"/>
      </xsl:template>
      
      <xsl:template match="Link_Process_App[key('app', @app_id)/@XorY = 'X']">
                        <Link_Process_Application>
                            <SourceKey>
                                <xsl:value-of select="@process_id"/>
                            </SourceKey>
                            <TargetKey>
                                <xsl:value-of select="@app_id"/>
                            </TargetKey>
                        </Link_Process_Application>      
      </xsl:template>
      
      <xsl:template match="Link_Process_App[key('app', @app_id)/@XorY = 'Y']">
                                            <Link_Process_IDP>
                            <SourceKey>
                                <xsl:value-of select="@process_id"/>
                            </SourceKey>
                            <TargetKey>
                                <xsl:value-of select="@app_id"/>
                            </TargetKey>
                        </Link_Process_IDP>   
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/bEJbVra/1

    【讨论】:

    • 你定义了&lt;xsl:key name="process" match="Process" use="@id"/&gt;,但我看不出你在哪里使用它。
    • @michael.hor257k,确实如此,不知何故,我最初认为需要交叉引用这两种类型的元素,然后我没有注意到我没有使用它就在那里拥有钥匙。我猜是移动世界中小屏幕的乐趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多