Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java xslt-can';t使用属性选择器访问当前节点_Java_Xml_Xslt_Xpath - Fatal编程技术网

Java xslt-can';t使用属性选择器访问当前节点

Java xslt-can';t使用属性选择器访问当前节点,java,xml,xslt,xpath,Java,Xml,Xslt,Xpath,我正在尝试将带有xsl样式表的xml文件转换为html 这是java TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(classLoader.getResourceAsStream("driving.xsl"))); StreamResu

我正在尝试将带有xsl样式表的xml文件转换为html

这是java

TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new StreamSource(classLoader.getResourceAsStream("driving.xsl")));
            StreamResult drivingHtml = new StreamResult(new StringWriter());
            transformer.transform(new StreamSource(classLoader.getResourceAsStream("driving.xml")), drivingHtml);
            System.out.println(drivingHtml.getWriter().toString());
以下是一些xml:

<?xml version="1.0" encoding="UTF-8"?>
<user xmlns="http://notreal.org/ns1" xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street> Elm St.</street>
    </address>

法院街1031号。
纽约州蒙赫根
埃尔姆街。
这是xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <head>
            <title>User</title>
            </head>
             <body>
                       <p>Detailed Addresses</p>
                       <table>
                 <th>Primary</th>
                 <th>Secondary</th>
                          <tr>
                <xsl:apply-templates select="/user/address"/>
                          </tr>
                         </table>
             </body>
         </html>
    </xsl:template>

     <xsl:template match="address">
          <td>
             <xsl:value-of select=".[@type='primary']/street" />
             <xsl:value-of select=".[@type='secondary']/street" />
          </td>
          <td>
             <xsl:value-of select=".[@type='primary']/city" />
             <xsl:value-of select=".[@type='secondary']/city" />
          </td>
     </xsl:template>
</xsl:stylesheet>

使用者
详细地址

主要的,重要的 次要的

当我运行它时,我得到“无法编译样式表”

根据提供的XML和XSLT代码,您的主要问题是,您的代码根本无法解决XML文档中的元素位于默认名称空间中这一事实

如何使用默认名称空间处理XML文档是一个常见问题——只要搜索xslt和xpath标记,您就会找到许多好的答案

这里有一个可能的解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>
此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

使用者
详细地址

应用于包含问题中提供的XML片段的完整且格式良好的XML文档时

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

法院街1031号。
纽约州蒙赫根
埃尔姆街203号。
宾夕法尼亚州匹兹堡
产生(似乎是)想要的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

使用者
详细地址

主要的,重要的 次要的 法院街1031号。 埃尔姆街203号。 纽约州蒙赫根 宾夕法尼亚州匹兹堡
基于提供的XML和XSLT代码,您的主要问题是,您的代码根本无法解决XML文档中的元素位于默认命名空间中这一事实

如何使用默认名称空间处理XML文档是一个常见问题——只要搜索xslt和xpath标记,您就会找到许多好的答案

这里有一个可能的解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>
此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

使用者
详细地址

应用于包含问题中提供的XML片段的完整且格式良好的XML文档时

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

法院街1031号。
纽约州蒙赫根
埃尔姆街203号。
宾夕法尼亚州匹兹堡
产生(似乎是)想要的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://notreal.org/ns1"
 exclude-result-prefixes="x">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
    <head>
     <title>User</title>
    </head>
    <body>
      <p>Detailed Addresses</p>
       <table>
        <thead>
         <xsl:apply-templates select="x:address/@type"/>
        </thead>
        <tr>
         <xsl:apply-templates select="x:address/x:street"/>
        </tr>
        <tr>
         <xsl:apply-templates select="x:address/x:city"/>
        </tr>
       </table>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="@type">
  <th><xsl:value-of select="."/></th>
 </xsl:template>

 <xsl:template match="x:address/*">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
    <address type="primary">
        <street>1031 Court St.</street>
        <city>Monhegan, NY</city>
    </address>

    <address type="secondary">
        <street>203 Elm St.</street>
        <city>Pittsburgh, PA</city>
    </address>
</user>
<html>
   <head>
      <title>User</title>
   </head>
   <body>
      <p>Detailed Addresses</p>
      <table>
         <thead>
            <th>primary</th>
            <th>secondary</th>
         </thead>
         <tr>
            <td>1031 Court St.</td>
            <td>203 Elm St.</td>
         </tr>
         <tr>
            <td>Monhegan, NY</td>
            <td>Pittsburgh, PA</td>
         </tr>
      </table>
   </body>
</html>

使用者
详细地址

主要的,重要的 次要的 法院街1031号。 埃尔姆街203号。 纽约州蒙赫根 宾夕法尼亚州匹兹堡