使用XSLT / Xpath打印值

时间:2014-01-06 11:32:08

标签: xml 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更改原始代码。

我没有在表格中显示两个单词,而是得到了这个:

What I get

除非有一些愚蠢的东西我缺席 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>

1 个答案:

答案 0 :(得分: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表达式中的无前缀节点名称始终引用名称空间中的节点。

与此无关,我个人会避免使用与“第一个......的第三个wd”的XML文档的精确布局密切相关的XPath表达式 - 您可以改为使用

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

专门针对前两个wd为“增值税”和“否:”的wd,无论发生在何处。