簡體   English   中英

使用XSLT / Xpath打印值

[英]Printing a value using XSLT/Xpath

我正在使用XSLT處理XML文檔,但我無法使用它。

這是我的簡單XSLT文檔。 我使用過w3schools的模板。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h1>VAT No.:</h1>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Prefix</th>
        <th>Number</th>
      </tr>
      <tr>
        <td><xsl:value-of select="/document/page/body/dd[1]/dd[1]/dd/table/cell[1]/para[1]/ln[1]/wd[3]" /></td>
        <td><xsl:value-of select="/document/page/body/dd[1]/dd[1]/dd/table/cell[1]/para[1]/ln[1]/wd[4]" /></td>
      </tr>
    </table> 
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

如你所見,我只是試圖顯示給出的兩個單詞。 我只是以表格格式保留它,以便盡可能少地從w3schools更改原始代碼。

我得到了這個:而不是在表格中顯示兩個單詞:

我得到了什么

除非有一些愚蠢的東西,否則 Xpaths是正確的。 我甚至使用在線Xpath查看器來確認它。 我嘗試過/,*和//的各種展示位置。 以及編輯線。

這是輸入XML的一部分。 由於存在一些敏感信息,因此不完整,但您可以看到布局。

<?xml version="1.0" encoding="UTF-16"?>
<?xml-stylesheet href="XSLT Stylesheet.XSL" type="text/xsl"?>
<!--XML document generated using OCR technology from Nuance Communications, Inc.-->
<document xmlns="http://www.scansoft.com/omnipage/xml/ssdoc-schema3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<page ocr-vers="OmniPageCSDK18" app-vers="OmniPage 19">
<description>
<source file="C:\Users\dan\Pictures\7740411202.jpg" dpix="300" dpiy="300" sizex="3375" sizey="2330"/>
<theoreticalPage size="Custom" marginLeft="134" marginTop="40" marginRight="45" marginBottom="1184" offsetY="-84" width="16200" height="11184"/>
<language>en</language>
</description>
<body>
<section l="14275" t="40" r="15715" b="228">
<column l="14275" t="40" r="15715" b="228">
<para l="14308" t="84" r="15697" b="197" alignment="left" spaceBefore="2" lsp="exactly" lspExact="174" language="en">
<tabs position="14308"/>
<ln l="14669" t="84" r="15631" b="197" baseLine="182">
<run underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c"><wd l="0" t="0" r="0" b="0">Page</wd>
<tab position="14669"/>
</run>
<run underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c"><wd l="15185" t="89" r="15218" b="194">1</wd>
<space/>
<wd l="15326" t="84" r="15470" b="197">of</wd>
<space/>
<wd l="15595" t="89" r="15631" b="194">1</wd>
</run>
</ln>
</para>
</column>
</section>
<dd l="139" t="266" r="8777" b="3762">
<dd l="4297" t="266" r="8777" b="1807" backColor="d3d3d2" bottomDistance="375">
<bottomBorder type="single" width="2" color="d7d7d7"/>
<dd l="4297" t="266" r="8777" b="1807">
<table l="4297" t="266" r="8777" b="1803" alignment="left">
<gridTable>
<gridCol>3899</gridCol>
<gridCol>581</gridCol>
<gridRow>1537</gridRow>
</gridTable>
<cell gridColFrom="0" gridColTill="0" gridRowFrom="0" gridRowTill="0" alignment="left" verticalAlignment="top">
<para l="4373" t="367" r="6322" b="679" alignment="left" li="72" spaceBefore="53" lsp="exactly" lspExact="197" language="en">
<ln l="4373" t="367" r="6322" b="482" baseLine="480" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c" forcedEOF="true">
<wd l="4373" t="367" r="4572" b="480">HP</wd>
<space/>
<wd l="4618" t="367" r="4944" b="480">VAT</wd>
<space/>
<wd l="4985" t="370" r="5213" b="482">No:</wd>
<space/>
<wd l="5388" t="367" r="6322" b="480">GB31414xxx
</wd>
</ln>
<ln l="4373" t="562" r="5738" b="679" baseLine="677" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c" forcedEOF="true">
<wd l="4373" t="562" r="5446" b="679">Hewlett-Packard</wd>
<space/>
<wd l="5491" t="562" r="5738" b="677">Ltd.</wd>
</ln>
</para>
<para l="4375" t="744" r="7368" b="1042" alignment="left" li="72" ri="828" lsp="exactly" lspExact="182" language="en">
<ln l="4375" t="744" r="7368" b="883" baseLine="859" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="-2" foreColor="0c0c0c">
<wd l="4375" t="744" r="4673" b="859">Cxxx</wd>
<space/>
<wd l="4718" t="744" r="5083" b="883">Rxxx,</wd>
<space/>
<wd l="5134" t="749" r="5522" b="862">Axxxx</wd>
<space/>
<wd l="5566" t="746" r="6007" b="859">Cxxxxx</wd>
<space/>
<wd l="6055" t="746" r="6701" b="883">Bracknell,</wd>
<space/>
<wd l="6754" t="744" r="7368" b="859">Berkshire</wd>
<space/>
</ln>
<ln l="4375" t="929" r="5100" b="1042" baseLine="1042" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="-2" foreColor="0c0c0c">
<wd l="4375" t="929" r="4747" b="1042">Rxxx</wd>
<space/>
<wd l="4812" t="929" r="5100" b="1042">1xx</wd>
</ln>
</para>
<para l="4375" t="1169" r="5590" b="1282" alignment="left" li="72" spaceBefore="61" lsp="exactly" lspExact="179" language="en">
<ln l="4375" t="1169" r="5590" b="1282" baseLine="1282" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c">
<wd l="4375" t="1169" r="4901" b="1282">GREAT</wd>
<space/>
<wd l="4949" t="1171" r="5590" b="1282">BRITAIN</wd>
</ln>
</para>
<para l="4375" t="1406" r="6528" b="1550" alignment="left" li="72" spaceBefore="61" spaceAfter="242" lsp="exactly" lspExact="179" language="en">
<ln l="4375" t="1406" r="6528" b="1550" baseLine="1522" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c">
<wd l="4375" t="1406" r="4766" b="1522">Phone</wd>
<space/>
<wd l="4810" t="1406" r="5366" b="1522">Number:</wd>
<space/>
<wd l="5395" t="1411" r="5597" b="1550">(44</wd>
<space/>
<wd l="5664" t="1409" r="6007" b="1550">1xxx)</wd>
<space/>
<wd l="6062" t="1409" r="6528" b="1522">36xxxxx</wd>
</ln>
</para>
</cell>
<cell gridColFrom="1" gridColTill="1" gridRowFrom="0" gridRowTill="0" alignment="centered" verticalAlignment="top">
<picture l="8196" t="305" r="8738" b="1769" alignment="centered" ri="39" spaceBefore="39" spaceAfter="34">
</picture>
</cell>
</table>
</dd>
</dd>
<dd l="4297" t="2182" r="8777" b="3762">
<para l="6444" t="2215" r="8683" b="2345" alignment="justified" li="2088" lsp="exactly" lspExact="207" language="en">
<ln l="6444" t="2215" r="8683" b="2345" baseLine="2342">
<run bold="true" underlined="none" subsuperscript="none" fontSize="900" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="7" foreColor="0c0c0c"><wd l="6444" t="2218" r="7390" b="2345">DELIVERY</wd>
<space/>
</run>
<run bold="true" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="7" foreColor="0c0c0c"><wd l="7447" t="2220" r="7692" b="2342">TO</wd>
<space/>
<wd l="7795" t="2220" r="8683" b="2342">0000610727</wd>
</run>
</ln>
</para>
<para l="6449" t="2496" r="7646" b="2628" alignment="justified" li="2088" spaceBefore="84" lsp="exactly" lspExact="195" language="en">
<ln l="6449" t="2496" r="7646" b="2628" baseLine="2626">
<run bold="true" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="2" foreColor="0c0c0c"><wd l="6449" t="2496" r="7034" b="2628">Cxxxxx</wd>
<space/>
<wd l="7082" t="2503" r="7246" b="2626">IT</wd>
<space/>
</run>
<run underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="2" foreColor="0c0c0c"><wd l="7298" t="2506" r="7646" b="2626">Parts</wd>
</run>
</ln>
</para>
<para l="6444" t="2736" r="7258" b="2868" alignment="justified" li="2088" spaceBefore="38" lsp="exactly" lspExact="207" language="en">
<ln l="6444" t="2736" r="7258" b="2868" baseLine="2866" underlined="none" subsuperscript="none" fontSize="900" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c">
<wd l="6444" t="2736" r="6936" b="2868">xxxxxx</wd>
<space/>
<wd l="6982" t="2741" r="7258" b="2868">Hse</wd>
</ln>
</para>
<para l="6449" t="2976" r="7219" b="3108" alignment="justified" li="2088" spaceBefore="35" lsp="exactly" lspExact="207" language="en">
<ln l="6449" t="2976" r="7219" b="3108" baseLine="3108" underlined="none" subsuperscript="none" fontSize="900" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c">
<wd l="6449" t="2976" r="6965" b="3108">Oxxxx</wd>
<space/>
<wd l="7010" t="2976" r="7219" b="3108">Rd</wd>
</ln>
</para>
<para l="6454" t="3218" r="8198" b="3348" alignment="justified" li="2088" spaceBefore="31" lsp="exactly" lspExact="207" language="en">
<ln l="6454" t="3218" r="8198" b="3348" baseLine="3346">
<run underlined="none" subsuperscript="none" fontSize="900" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="1" foreColor="0c0c0c"><wd l="6454" t="3218" r="6739" b="3348">SL9</wd>
<space/>
<wd l="6794" t="3218" r="7745" b="3348">7Bxxxx</wd>
<space/>
</run>
<run underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="1" foreColor="0c0c0c"><wd l="7805" t="3223" r="8198" b="3346">Cross</wd>
</run>
</ln>
</para>
<para l="6449" t="3458" r="7810" b="3588" alignment="justified" li="2088" spaceBefore="33" spaceAfter="125" lsp="exactly" lspExact="207" language="en">
<ln l="6449" t="3458" r="7810" b="3588" baseLine="3586" underlined="none" subsuperscript="none" fontSize="900" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="0" foreColor="0c0c0c">
<wd l="6449" t="3458" r="7042" b="3588">GREAT</wd>
<space/>
<wd l="7092" t="3463" r="7810" b="3586">BRITAIN</wd>
</ln>
</para>
</dd>
<dd l="139" t="266" r="689" b="962">
<picture l="139" t="413" r="689" b="962" alignment="left" spaceBefore="147">
</picture>
</dd>
<dd l="139" t="2279" r="2508" b="3762">
<para l="149" t="2302" r="2105" b="2424" alignment="justified" lsp="exactly" lspExact="185" language="en">
<ln l="149" t="2302" r="2105" b="2424" baseLine="2422" bold="true" underlined="none" subsuperscript="none" fontSize="800" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="16" foreColor="0c0c0c">
<wd l="149" t="2302" r="518" b="2422">VAT</wd>
<space/>
<wd l="574" t="2304" r="833" b="2424">No:</wd>
<space/>
<wd l="1054" t="2302" r="2105" b="2424">GB530000000</wd>
</ln>
</para>
<document xmlns="http://www.scansoft.com/omnipage/xml/ssdoc-schema3.xsd"

此聲明將文檔中所有未加前綴的元素名稱放入命名空間,因此您需要在XSLT中為該命名空間分配前綴,然后在XPath表達式中使用該前綴:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:ss="http://www.scansoft.com/omnipage/xml/ssdoc-schema3.xsd">

  <!-- ... -->
  <td><xsl:value-of select="/ss:document/ss:page/ss:body/ss:dd[1]/ss:dd[1]/ss:dd/ss:table/ss:cell[1]/ss:para[1]/ss:ln[1]/ss:wd[3]" /></td>

這是因為XPath 1.0表達式中未加前綴的節點名稱始終引用不在命名空間中的節點。

與此無關,我個人會避免使用一個與XML文檔的精確布局密切相關的XPath表達式,即“第一個......的第三個wd” - 你可以改為使用像

<xsl:value-of select="//ss:wd[preceding-sibling::ss:wd[2] = 'VAT']
                             [preceding-sibling::ss:wd[1] = 'No:']" />

特異性靶向一個wd ,其兩個前述wd s為“VAT”和“No”,無論它發生時發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM