1

我正在尝试使用 XSLT 1.0 中的文档功能,但没有运气。

我正在转换以下 XML

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<items>
  <item>
    <item_number>ZZZ1112</item_number>
    <addl_descr_1><![CDATA[Fudge It Kit Item 1 TEST]]></addl_descr_1>
    <is_txbl>Y</is_txbl>
    <stk_unit>EACH</stk_unit>
    <item_vend_no>5338</item_vend_no>
    <weight>1.0000</weight>
    <prof_alpha_1><![CDATA[MPEX]]></prof_alpha_1>
    <prof_alpha_2><![CDATA[Nikon]]></prof_alpha_2>
    <prof_alpha_3><![CDATA[77mm]]></prof_alpha_3>
    <prof_alpha_4><![CDATA[Telephoto]]></prof_alpha_4>
    <prof_alpha_5><![CDATA[501-1000]]></prof_alpha_5>
  </item>
</items>

使用以下 xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="manufacturer-top" select="msxsl:node-set(document('manufacturer.xml'))" />

  <xsl:template match="/items">
    <items>
      <xsl:for-each select="item">
        <item>
          <xsl:variable name="man" select="prof_alpha_1" />
          <manufacturer>
            <xsl:value-of select="$man"/>
          </manufacturer>
          <id>
            <xsl:value-of select="$manufacturer-top/select[@id='manufacturer'][option = $man]/@value"/>
          </id>
        </item>
      </xsl:for-each>
    </items>
  </xsl:template>

</xsl:stylesheet>

并使用以下“查找” xml 文档

<?xml version="1.0" encoding="utf-8" ?>
<select id="manufacturer">
  <option value=""></option>
  <option value="476">Abdi Roble</option>
  <option value="477">ActionRunner</option>
  <option value="395">Acufine</option>
  <option value="2">Adobe</option>
  <option value="515">Agfa</option>
  <option value="390">Aja</option>
  <option value="475">AJA Video Systems</option>
  <option value="514">Albinar</option>
  <option value="513">Allary</option>
  <option value="113">Amherst Media</option>
  <option value="386">Ansman</option>
  <option value="391">AquaTech</option>
  <option value="112">Arca Swiss</option>
  <option value="474">Arch and Pillar</option>
  <option value="512">Argus</option>
  <option value="111">Aristo</option>
  <option value="110">Arkay</option>
  <option value="473">Arri</option>
  <option value="406">Audio-Tecnica </option>
  <option value="358">Aurora</option>
  <option value="109">Aurora</option>
  <option value="108">Avenger</option>
  <option value="107">Azden</option>
  <option value="106">B+W</option>
  <option value="399">b-grip</option>
  <option value="393">Ban Dust</option>
  <option value="105">Beach Tek</option>
  <option value="104">Beattie</option>
  <option value="103">Belkin</option>
  <option value="102">Benchor</option>
  <option value="101">Benro</option>
  <option value="396">Berg </option>
  <option value="100">Bergger</option>
  <option value="99">Bescor</option>
  <option value="98">Besler</option>
  <option value="97">Bestwell</option>
  <option value="96">Bibble</option>
  <option value="95">Black Rapid</option>
  <option value="526">Blackmagic Design</option>
  <option value="94">Blue Crane</option>
  <option value="93">Bongo</option>
  <option value="398">BOOTH Photographic Limited</option>
  <option value="397">Bostick &amp; Sullivan</option>
  <option value="92">Bowens</option>
  <option value="91">Bower</option>
  <option value="405">Bron Imaging Group</option>
  <option value="472">Broncolor</option>
  <option value="404">C&amp;R Enterprises</option>
  <option value="471">Cables To Go</option>
  <option value="403">Cables Umlimited</option>
  <option value="90">Cactus</option>
  <option value="89">Calumet</option>
  <option value="402">CAM Engineering</option>
  <option value="88">Cameron</option>
  <option value="87">Canham</option>
  <option value="86">Canon</option>
  <option value="362">Capture One</option>
  <option value="401">Casio</option>
  <option value="369">CBL</option>
  <option value="400">CENGAGE Learning </option>
  <option value="413">Cescolite</option>
  <option value="85">Chimera</option>
  <option value="412">Cinevate</option>
  <option value="84">Clear File</option>
  <option value="83">Cokin</option>
  <option value="82">Colorbyte</option>
  <option value="81">Colorspace</option>
  <option value="80">Colorvision</option>
  <option value="470">Contour</option>
  <option value="79">Copal</option>
  <option value="78">Corel</option>
  <option value="77">Crane</option>
  <option value="76">Creative Light</option>
  <option value="75">CTA</option>
  <option value="74">Custom Bracket</option>
  <option value="481">D &amp; P Media</option>
  <option value="72">Da-lite</option>
  <option value="73">Dahle</option>
  <option value="519">darkroom software</option>
  <option value="520">Datacolor</option>
  <option value="522">Datacolor</option>
  <option value="71">Davis &amp; Sanford</option>
  <option value="70">Dayton</option>
  <option value="115">Deardorff</option>
  <option value="114">Dedolight</option>
  <option value="155">Delkin</option>
  <option value="383">Delta</option>
  <option value="511">Denny</option>
  <option value="411">Dexter</option>
  <option value="154">Diana</option>
  <option value="410">Digital Concepts</option>
  <option value="409">DigiView</option>
  <option value="480">DIY Photography</option>
  <option value="479">DNP</option>
  <option value="153">Domke</option>
  <option value="382">Domke</option>
  <option value="408">Doran</option>
  <option value="152">Dot Line</option>
  <option value="374">Drew Gardner</option>
  <option value="151">Drobo</option>
  <option value="407">drop it MODERN</option>
  <option value="150">Dyna-lite</option>
  <option value="425">E-Filming</option>
  <option value="149">Ebony</option>
  <option value="381">Ebony</option>
  <option value="148">Edwal</option>
  <option value="147">Eiko</option>
  <option value="146">Eizo</option>
  <option value="145">Elinchrom</option>
  <option value="364">eMotion</option>
  <option value="379">Energizer</option>
  <option value="424">Epoque USA</option>
  <option value="144">Epson</option>
  <option value="143">Ethol</option>
  <option value="142">Expodisc</option>
  <option value="141">Eye-Fi</option>
  <option value="140">Ezy Grip</option>
  <option value="420">F-Stop Academy</option>
  <option value="378">Fastenal</option>
  <option value="509">Fiberbilt</option>
  <option value="423">Fiberbuilt</option>
  <option value="139">Fidelity</option>
  <option value="138">Flextight</option>
  <option value="422">Flip</option>
  <option value="137">Focal Press</option>
  <option value="136">Fomex</option>
  <option value="135">Fotodiox</option>
  <option value="377">Fotoman</option>
  <option value="134">Fotomann</option>
  <option value="421">Frame USA</option>
  <option value="478">Fred Lustig</option>
  <option value="488">Fred Marsh</option>
  <option value="487">Frio</option>
  <option value="133">Fuji</option>
  <option value="486">Gagne</option>
  <option value="419">GamiLight Concepts</option>
  <option value="132">Ganz</option>
  <option value="131">Garmin</option>
  <option value="130">Gary Fong</option>
  <option value="129">Gator</option>
  <option value="418">GenAmerica</option>
  <option value="128">Genus</option>
  <option value="127">Gepe</option>
  <option value="375">Gigapan</option>
  <option value="126">Giotto</option>
  <option value="485">GiSTEQ</option>
  <option value="417">Gisteq Corp.</option>
  <option value="125">Gitzo</option>
  <option value="508">Goerz</option>
  <option value="416">Goetschmann</option>
  <option value="521">GoPro</option>
  <option value="507">Gossen</option>
  <option value="124">Gralab</option>
  <option value="123">Graphic</option>
  <option value="376">Graphic Authority</option>
  <option value="122">Graphic Authority</option>
  <option value="415">Graslon</option>
  <option value="373">Gravity Works</option>
  <option value="121">Gretag</option>
  <option value="120">Hahnemuhle</option>
  <option value="384">Hama</option>
  <option value="119">Hanson</option>
  <option value="118">Harman</option>
  <option value="117">Harrison</option>
  <option value="116">Hasselblad</option>
  <option value="173">Heliopan</option>
  <option value="172">Hewlett Packard</option>
  <option value="170">Hi-Touch</option>
  <option value="171">Hildozine</option>
  <option value="372">HoldSLR</option>
  <option value="169">Honeywell</option>
  <option value="168">Honi</option>
  <option value="167">Hoodman</option>
  <option value="166">Horseman</option>
  <option value="484">Hosa</option>
  <option value="165">Hoya</option>
  <option value="164">Huey</option>
  <option value="163">Ikan</option>
  <option value="414">Ikeda Anba</option>
  <option value="162">Ilex</option>
  <option value="161">Ilford</option>
  <option value="160">Image Resourcs</option>
  <option value="159">Imedion</option>
  <option value="370">Impact</option>
  <option value="432">Impossible Project</option>
  <option value="158">Induro</option>
  <option value="157">InkPress</option>
  <option value="483">Innovatronix</option>
  <option value="156">Interfit</option>
  <option value="286">Itoya</option>
  <option value="285">JDI</option>
  <option value="284">Jill-E</option>
  <option value="283">Jobo</option>
  <option value="282">Joby</option>
  <option value="431">JuicedLink</option>
  <option value="281">Justrite</option>
  <option value="280">JVC</option>
  <option value="523">K-Tek</option>
  <option value="368">K-Tek</option>
  <option value="430">K.B. Canham Cameras</option>
  <option value="279">Kacey</option>
  <option value="278">Kaiser</option>
  <option value="277">Kalt</option>
  <option value="276">Kata</option>
  <option value="482">Kelly Moore</option>
  <option value="275">Kenko</option>
  <option value="367">Kiesel</option>
  <option value="274">Kingston</option>
  <option value="273">Kodak</option>
  <option value="272">Kostiner</option>
  <option value="525">Kupo</option>
  <option value="271">Lacie</option>
  <option value="270">Larson</option>
  <option value="269">Lastolite</option>
  <option value="268">Leaf</option>
  <option value="267">Lee</option>
  <option value="266">Leedal</option>
  <option value="429">Legacy Kentmere</option>
  <option value="265">Leica</option>
  <option value="264">Lenmar</option>
  <option value="263">Lensbaby</option>
  <option value="428">LensCoat</option>
  <option value="262">Lexar</option>
  <option value="261">Lexican</option>
  <option value="260">Light Impressions</option>
  <option value="427">Lindahl</option>
  <option value="259">Linhof</option>
  <option value="258">Litepanel</option>
  <option value="257">Litepanels</option>
  <option value="256">Logan</option>
  <option value="255">Lowel</option>
  <option value="254">Lowepro</option>
  <option value="253">Lumedyne</option>
  <option value="252">Lumiquest</option>
  <option value="251">Lumopro</option>
  <option value="250">Mack</option>
  <option value="493">Magazine</option>
  <option value="249">Magic Lantern</option>
  <option value="248">Mamiya</option>
  <option value="247">Manfrotto</option>
  <option value="510">Manufacturing</option>
  <option value="426">Marathon</option>
  <option value="246">Marflex</option>
  <option value="245">Marshall</option>
  <option value="392">Marumi</option>
  <option value="244">Matthews</option>
  <option value="365">Meade</option>
  <option value="440">MemoryKick</option>
  <option value="243">Metz</option>
  <option value="242">Micro-sight</option>
  <option value="345">MicroGaffer</option>
  <option value="241">Microsync</option>
  <option value="240">Microtek</option>
  <option value="239">Minolta</option>
  <option value="238">Minox</option>
  <option value="366">Monster</option>
  <option value="237">Morrison</option>
  <option value="439">Motif</option>
  <option value="380">MPEX</option>
  <option value="438">Museo</option>
  <option value="236">NEC</option>
  <option value="437">Nexto</option>
  <option value="235">NIK</option>
  <option value="234">Nikon</option>
  <option value="387">Nisha</option>
  <option value="233">Nisha</option>
  <option value="232">Nissin</option>
  <option value="436">Nite Ize</option>
  <option value="231">Norman</option>
  <option value="506">Nova</option>
  <option value="230">Novoflex</option>
  <option value="435">nuCourse</option>
  <option value="229">Olympus</option>
  <option value="228">Omega</option>
  <option value="227">onOne</option>
  <option value="226">Optech</option>
  <option value="225">Opus</option>
  <option value="224">Orbis</option>
  <option value="223">Orient</option>
  <option value="434">Pana-Scan</option>
  <option value="394">Pana-Vue</option>
  <option value="222">Panasonic</option>
  <option value="433">Pandigital</option>
  <option value="465">Pantone</option>
  <option value="221">Paramount</option>
  <option value="220">Paterson</option>
  <option value="363">Peach Pit</option>
  <option value="219">Peak</option>
  <option value="359">Pearstone</option>
  <option value="464">Pearstone</option>
  <option value="218">Pec</option>
  <option value="463">Peerless</option>
  <option value="217">Pelican</option>
  <option value="216">Pentax</option>
  <option value="215">Phase One</option>
  <option value="214">Phoenix</option>
  <option value="462">Photo Master</option>
  <option value="360">Photo Vision</option>
  <option value="213">Photoflex</option>
  <option value="212">Photogenic</option>
  <option value="461">Photogizzmo</option>
  <option value="211">Photographic Solutions</option>
  <option value="361">Photographic Solutions</option>
  <option value="460">PhotoshopCAFE</option>
  <option value="459">Phottix</option>
  <option value="210">Pocket Wizard</option>
  <option value="458">Polaris</option>
  <option value="209">Polaroid</option>
  <option value="208">Pony</option>
  <option value="207">Powerex</option>
  <option value="505">Praktica</option>
  <option value="206">Premier</option>
  <option value="205">Premier Art</option>
  <option value="204">Print File</option>
  <option value="203">Profoto</option>
  <option value="504">Promaster</option>
  <option value="457">ProMold</option>
  <option value="456">Proof File</option>
  <option value="455">Provista</option>
  <option value="503">QP Card</option>
  <option value="502">Quantaray</option>
  <option value="202">Quantum</option>
  <option value="201">Radiopopper</option>
  <option value="200">Ray Flash</option>
  <option value="199">Raynox</option>
  <option value="198">RedRock</option>
  <option value="197">RedWing</option>
  <option value="196">Reflexite</option>
  <option value="454">RepairTech</option>
  <option value="195">Ricoh</option>
  <option value="194">Ries</option>
  <option value="193">Riteway</option>
  <option value="192">RoadWire</option>
  <option value="191">Rockland</option>
  <option value="190">Rode</option>
  <option value="189">Rodenstock</option>
  <option value="516">Rogue</option>
  <option value="188">Rollei</option>
  <option value="187">Rosco</option>
  <option value="186">RotaTrim</option>
  <option value="357">RPS</option>
  <option value="453">Ruige</option>
  <option value="185">Rycote</option>
  <option value="452">Sable</option>
  <option value="356">Safari</option>
  <option value="184">Saflin</option>
  <option value="183">Samigon</option>
  <option value="182">Samson</option>
  <option value="329">Samson</option>
  <option value="181">Sandisk</option>
  <option value="501">Sanford</option>
  <option value="180">Sanyo</option>
  <option value="179">Saunders</option>
  <option value="178">Savage</option>
  <option value="500">Sawyer's</option>
  <option value="177">Schneider</option>
  <option value="451">Sea &amp; Sea</option>
  <option value="176">Seal</option>
  <option value="385">Seculine</option>
  <option value="175">Seculine</option>
  <option value="174">Seinheiser</option>
  <option value="328">Sekonic</option>
  <option value="354">Sharp</option>
  <option value="327">Shen Hao</option>
  <option value="355">Shortcut</option>
  <option value="353">Shure</option>
  <option value="352">Shutterbug</option>
  <option value="326">Sigma</option>
  <option value="325">Sima</option>
  <option value="324">Sinar</option>
  <option value="323">Singhray</option>
  <option value="322">Skyla</option>
  <option value="321">Smith Victor</option>
  <option value="499">Soligor</option>
  <option value="351">Sonnett</option>
  <option value="320">Sony</option>
  <option value="498">Spectra Cine</option>
  <option value="319">Speedlight</option>
  <option value="318">Speedotron</option>
  <option value="497">Spiratone</option>
  <option value="317">Sprint</option>
  <option value="489">Spudz</option>
  <option value="316">Startech</option>
  <option value="388">Steadepod</option>
  <option value="315">Sto-fen</option>
  <option value="314">Stroboframe</option>
  <option value="313">Studio Titan</option>
  <option value="312">SunBounce</option>
  <option value="311">Sunpak</option>
  <option value="310">Superior</option>
  <option value="350">Switronics</option>
  <option value="450">Sylvestri</option>
  <option value="349">Synergy</option>
  <option value="309">Tachihara</option>
  <option value="308">Tamrac</option>
  <option value="348">Tamrac</option>
  <option value="307">Tamron</option>
  <option value="306">TAP</option>
  <option value="449">Technika</option>
  <option value="496">Tele-Astranar</option>
  <option value="305">Tenba</option>
  <option value="304">Testrite</option>
  <option value="303">Think Tank</option>
  <option value="302">Thomas</option>
  <option value="301">Tiffen</option>
  <option value="300">Tiltall</option>
  <option value="299">Tim Kelly</option>
  <option value="298">Tokina</option>
  <option value="297">Toyo</option>
  <option value="448">Trade Secret</option>
  <option value="296">Transcend</option>
  <option value="295">Trek-Tech</option>
  <option value="347">Trektek</option>
  <option value="294">Tripp Lite</option>
  <option value="447">Tundra</option>
  <option value="495">Ultranar</option>
  <option value="492">V-Pan</option>
  <option value="346">Vanguard</option>
  <option value="446">Varavon</option>
  <option value="445">Varta</option>
  <option value="344">Veho</option>
  <option value="343">Versaflex</option>
  <option value="444">Versatile Studios</option>
  <option value="443">Versatools</option>
  <option value="341">Vertus</option>
  <option value="494">View Camera</option>
  <option value="442">Virtual Backgrounds</option>
  <option value="293">Visible Dust</option>
  <option value="342">Visual Echoes</option>
  <option value="441">Vivitar</option>
  <option value="469">Voigtlander</option>
  <option value="340">Vue-All</option>
  <option value="339">Wacom</option>
  <option value="292">Wacom</option>
  <option value="468">Walker Studio</option>
  <option value="338">Walkstool</option>
  <option value="337">Wein</option>
  <option value="291">Westcott</option>
  <option value="336">Westcott</option>
  <option value="335">Wimberley</option>
  <option value="467">Wisner</option>
  <option value="334">Wista</option>
  <option value="491">WOWee ONE</option>
  <option value="290">X-Acto</option>
  <option value="466">X-Keys</option>
  <option value="524">X-Rite</option>
  <option value="289">Yankee</option>
  <option value="490">Yashica</option>
  <option value="333">Zacuto</option>
  <option value="331">Zeikos</option>
  <option value="288">Zeiss</option>
  <option value="332">Zeiss</option>
  <option value="287">Zigview</option>
  <option value="330">Zone VI</option>
</select>

我要完成的要点是从 prof_alpha_1 值转换 MPEX 值并从查找 xml 中获取 380 值,但不幸的是我的结果是

<?xml version="1.0" encoding="utf-8"?>
<items>
  <item>
    <manufacturer>MPEX</manufacturer>
    <id></id>
  </item>
</items>

每次我尝试调试变量制造商顶部时,该值始终是查找 xml 的值,而不是我可以使用 XPath 的节点集。任何帮助深表感谢!

4

1 回答 1

3

document()select="document(...返回一个节点集,因此不应该是select="msxsl:node-set(documentnode-set() 采用结果树片段并返回一个节点集,因此这里不需要,

但你真正的错误是

select="$manufacturer-top/select[@id='manufacturer'][option = $man]/@value"/>

这是选择你想要的value属性select

    select="$manufacturer-top/select[@id='manufacturer']/option[. = $man]/@value"/>

尽管只要你有xxx/foo[.=$bar],你可能会通过使用密钥来获得很大的加速。

将此添加到样式表

<xsl:key name="op" match="option" use="."/>

然后替换

<xsl_value-of select="$manufacturer-top/select[@id='manufacturer']/option[. = $man]/@value"/>

经过

<xsl:for-each select="$manufacturer-top">
  <xsl:value-of select="key('op',$man)/@value"/>
</xsl:for-each>

除非您的 xslt 优化器很聪明,否则第一个优化器会在每个 tine 对整个列表进行线性搜索,第二个将读取列表一次并制作一个快速散列查找表。

于 2013-03-08T14:52:12.517 回答