I was trying to get sibling of xml look up file ba

2019-03-04 14:30发布

问题:

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>

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>

Here is my 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

回答1:

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

Given this input 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>

and a second file named templates.xml:

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

the following stylesheet:

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>

will return:

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

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 xslt xpath