簡體   English   中英

如何從xml刪除前綴/命名空間

[英]How to remove prefix/namespace from xml

我有以下xml文件:

<q1:GeneralAgenda xmlns="http://schemas.gov.sk/form/Notify.GeneralAgenda/1.1">
  <q1:subject>text text text</q1:subject>
  <q1:text>lorem ipsum</q1:text>
</q1:GeneralAgenda>

我創建了xsl文件:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ga="http://schemas.gov.sk/form/Notify.GeneralAgenda/1.1">


<xsl:template match="ga:GeneralAgenda">
  <html>
   <head>

    </head> 
  <body>

   <div id="main" class="layoutMain">
      <div class="layoutRow ui-tabs ui-widget-content">
         <div class="caption ui-widget-header">
            <div class="headercorrection">Všeobecná agenda - oznámenie</div>
         </div>

         <div><label class="labelVis">Predmet: </label>

           <span class="contentVis wordwrap">

             <xsl:value-of select="ga:subject"/>

           </span>

        </div>

         <div class="clear"> </div>
         <div><label class="labelVis">Text: </label>
           <span class="contentVis wordwrap">

            <xsl:value-of select="ga:text"/>

           </span>
         </div>
         <div class="clear"> </div>
      </div>
   </div>

  </body>

  </html>
</xsl:template>

</xsl:stylesheet>

問題是由於xml文件中的“ q1:”部分而無法使用,我該如何解決? 我正在使用Java從xml文件生成hmtl文件,當我手動刪除q1時,它工作正常,我認為有一種方法可以調整xsl文件,但我只是不知道如何。

一般規則是:

  • 從XSLT腳本中刪除顯式的“用戶”名稱空間(但保留xsl名稱空間前綴)。
  • 使用*[local-name() = '...']指定matchselect屬性。

因此,您可以使用如下所示的腳本,該腳本是經過重新設計的版本。

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.1//EN"
    doctype-system= "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

  <xsl:template match="*[local-name() = 'GeneralAgenda']">
    <html>
      <head>
      </head> 
      <body>
        <div id="main" class="layoutMain">
          <div class="layoutRow ui-tabs ui-widget-content">
            <div class="caption ui-widget-header">
              <div class="headercorrection">Všeobecná agenda - oznámenie</div>
            </div>
            <div>
              <label class="labelVis">Predmet: </label>
              <span class="contentVis wordwrap">
                <xsl:value-of select="*[local-name() = 'subject']"/>
              </span>
            </div>
            <div class="clear"> </div>
            <div>
              <label class="labelVis">Text: </label>
              <span class="contentVis wordwrap">
                <xsl:value-of select="*[local-name() = 'text']"/>
              </span>
            </div>
            <div class="clear"> </div>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

有關工作示例,請參見http://xsltransform.net/pNvs5w3/1

注意上面給出的示例中的源XML內容。 xmlns聲明必須包含:q1前綴。

<q1:GeneralAgenda xmlns:q1="urn:dummy_q1">

否則,將導致您的源XML格式不正確,甚至Xalan這樣的“基本”實現也會失敗。

暫無
暫無

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

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