我试图根据特定条件获取 xml 查找文件的兄弟 [英] I was trying to get sibling of xml look up file based on specfic condition

查看:19
本文介绍了我试图根据特定条件获取 xml 查找文件的兄弟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个查找文件,用于获取与实际 xml 文件中匹配的表属性的值

Here is a lookup file get the values for Table attributes that match in the actual xml files

 <?xml version="1.0"?>
<Templates>
<Template>
<Obj>1</Ob>
<Att>1</Att>
<Ingest>0</Ingest>
<Name>FNC</Name>
<Category>ALL</Category>
<Class>TestTwo</Class>
<Table>CNDB0</Table>
<Attribute>cod</_Attribute>
<Value>-22</Value>
</Template>
</Templates>

这里是实际的xml文件

Here is the actual xml file

 <?xml version="1.0" encoding="UTF-8"?><p:transformOutput xmlns:p="http://cfpe/export/objects">
 <p:objectSet>
 <p:objectSetType>DNC</p:objectSetType>
 <p:objects>
 <p:object>
 <p:objectType>FNC</p:objectType>
 <p:objectAttributes>
 <p:attribute name="ssc">10</p:attribute>
 <p:attribute name="btc">0</p:attribute>
 <p:attribute name="ccc">27</p:attribute>
 <p:attribute name="nam">C</p:attribute>
 <p:attribute name="Code">0001</p:attribute>

 </p:objectAttributes>
 </p:object>
 </p:objects>
 </p:objectSet>
 </p:transformOutput>

这是我的 xsl

    <xsl:variable name="statClass"  select="$objectPath/Template/Class" />

                     <xsl:variable name="matchObject"  select="$valuePath/p:obje/p:objectType"/>

                        <xsl:choose>
                            <xsl:when test="$statClass =$matchObject">



                                <xsl:value-of select="$objectPath/Template/text()"/>

一旦其中一个属性匹配,我就无法选择模板中的所有属性

I couldn't get to select all the attributes in the template once one of the attributes matches

推荐答案

最好使用 来查找值.这是一个最小化示例:

It is best to use a key to lookup values. Here is a minimized example:

鉴于此输入 XML:

<?xml version="1.0" encoding="UTF-8"?>
<p:transformOutput xmlns:p="http://cfpe/export/objects">
   <p:objectSet>
      <p:objects>
         <p:object>
            <p:objectType>FNC</p:objectType>
          </p:object>
      </p:objects>
   </p:objectSet>
</p:transformOutput>

以及名为 templates.xml 的第二个文件:

and a second file named templates.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Templates>
   <Template>
      <Name>FNC</Name>
      <Value>-22</Value>
   </Template>
</Templates>

以下样式表:

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://cfpe/export/objects"
exclude-result-prefixes="p">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="template" match="Template" use="Name" />

<xsl:template match="/p:transformOutput/p:objectSet">
    <objects>
        <xsl:for-each select="p:objects/p:object">
            <xsl:variable name="objType" select="p:objectType" />
            <object type="{$objType}">
                <template>
                    <!-- switch context to the lookup file in order to use key -->
                    <xsl:for-each select="document('templates.xml')">
                            <xsl:copy-of select="key('template', $objType)/Value"/>
                    </xsl:for-each> 
                </template>
            </object>
        </xsl:for-each>     
    </objects>
</xsl:template>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
   <object type="FNC">
      <template>
         <Value>-22</Value>
      </template>
   </object>
</objects>

在 XSLT 2.0 中,您将能够使用查找文件作为 key() 函数的参数,从而避免需要切换上下文.

In XSLT 2.0 you'll be able use the lookup file as an argument of the key() function, thus avoiding the need to switch context.

这篇关于我试图根据特定条件获取 xml 查找文件的兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆