2014-01-06 79 views
0

我正在使用XSLT處理XML文檔,但無法使其工作。使用XSLT/Xpath打印值

這是我簡單的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中更改原始代碼。

而不必在表中顯示的兩個詞,我得到這個:

What I get

除非有一些愚蠢的我失蹤的XPath是正確的。我甚至使用在線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> 
+2

你能展示一個輸入XML的例子嗎?它是否使用任何名稱空間? –

回答

2
<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表達式,而不是「第一個ln的第三個wd」 - 您可以改爲使用類似

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

專門針對一個wd其前兩個wd s爲「增值稅」和「No」,無論它發生發生。

+0

「此聲明將文檔中所有未加前綴的元素名稱放入一個名稱空間中,因此您需要爲XSLT中的該名稱空間指定一個前綴,然後在XPath表達式中使用前綴」 如何更改它以便我不需要這樣做? – Dan

+0

@你不能在XSLT(1.0)中,你必須編輯輸入XML文件以刪除名稱空間聲明。如果你有權訪問XSLT 2.0,那麼你可以使用'xpath-default-namespace',但這在1.0中是不可用的(假設你使用了<?xml-stylesheet'我假設你使用瀏覽器來進行轉換,而瀏覽器只支持1.0)。 –

+0

我確實有能力編輯輸入XML文件。這是否像在開始時添加額外的線一樣簡單? – Dan