【问题标题】:XML to XML transformation using XSLT使用 XSLT 将 XML 转换为 XML
【发布时间】:2011-09-02 04:25:03
【问题描述】:

我正在尝试使用 XSLT 文件将一个 XML 文件转换为另一个 XML 文件。以下是源文件:-

源文件

<country isocode="de" pk="8796093055010" uri="http://localhost:9001/ws410/rest/countries/de">
        <comments/>
        <creationtime>2011-08-03T21:53:35.624+05:30</creationtime>
        <dimVals/>
        <modifiedtime>2011-08-03T22:05:10.111+05:30</modifiedtime>
        <active>true</active>
        <name>Germany</name>
        <regions>
              <region isocode="DE-BW" pk="8796093055011" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BW"/>
              <region isocode="DE-BY" pk="8796093087779" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BY"/>
              <region isocode="DE-BE" pk="8796093120547" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BE"/>
              <region isocode="DE-BR" pk="8796093153315" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BR"/>
              <region isocode="DE-HB" pk="8796093186083" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HB"/>
              <region isocode="DE-HH" pk="8796093218851" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HH"/>
              <region isocode="DE-HE" pk="8796093251619" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HE"/>
              <region isocode="DE-MV" pk="8796093284387" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-MV"/>
              <region isocode="DE-NI" pk="8796093317155" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-NI"/>
              <region isocode="DE-NW" pk="8796093349923" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-NW"/>
              <region isocode="DE-RP" pk="8796093382691" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-RP"/>
              <region isocode="DE-SL" pk="8796093415459" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SL"/>
              <region isocode="DE-ST" pk="8796093448227" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-ST"/>
              <region isocode="DE-SN" pk="8796093480995" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SN"/>
              <region isocode="DE-SH" pk="8796093513763" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SH"/>
              <region isocode="DE-TH" pk="8796093546531" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-TH"/>
        </regions>
        <zones>
            <zone code="de" pk="8796093056179" uri="http://localhost:9001/ws410/rest/zones/de"/>
        </zones>
      </country>

XSLT 文件

<xsl:template match="/country/regions">
    <RECORDS> 
        <xsl:for-each select="region">
            <RECORD>
                <PROP NAME="isocode">
                    <PVAL><xsl:value-of select="@isocode"/></PVAL>
                </PROP>
                <PROP NAME="pk">
                    <PVAL><xsl:value-of select="@pk"/></PVAL>
                </PROP>
                <PROP NAME="uri">
                    <PVAL><xsl:value-of select="@uri"/></PVAL>
                </PROP>
            </RECORD>
        </xsl:for-each>
    </RECORDS>
</xsl:template>

预期输出

<RECORDS> 
        <RECORD>
            <PROP NAME="isocode">
                <PVAL>DE-BW</PVAL>
            </PROP>
            <PROP NAME="pk">
                <PVAL>8796093055011</PVAL>
            </PROP>
            <PROP NAME="uri">
                <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BW</PVAL>
            </PROP>
        </RECORD>
        ...........
</RECORDS>

为了检查输出,我包含了&lt;?xml-stylesheet type="text/xsl" href="&lt;&lt;XSLT file&gt;&gt;"?&gt; 然后我在 Firefox 浏览器上打开文件。但输出如下所示,没有自定义标签,如 &lt;RECORDS&gt;&lt;RECORD&gt;&lt;PROP&gt;

2011-08-03T21:53:35.624+05:30

        2011-08-03T22:05:10.111+05:30
        true
        Germany
        DE-BW8796093055011http://localhost:9001/ws410/rest/countries/de/regions/DE-BWDE-BY8796093087779http://localhost:9001/ws410/rest/countries/de/regions/DE-BYDE-BE8796093120547http://localhost:9001/ws410/rest/countries/de/regions/DE-BEDE-BR8796093153315http://localhost:9001/ws410/rest/countries/de/regions/DE-BRDE-HB8796093186083http://localhost:9001/ws410/rest/countries/de/regions/DE-HBDE-HH8796093218851http://localhost:9001/ws410/rest/countries/de/regions/DE-HHDE-HE8796093251619http://localhost:9001/ws410/rest/countries/de/regions/DE-HEDE-MV8796093284387http://localhost:9001/ws410/rest/countries/de/regions/DE-MVDE-NI8796093317155http://localhost:9001/ws410/rest/countries/de/regions/DE-NIDE-NW8796093349923http://localhost:9001/ws410/rest/countries/de/regions/DE-NWDE-RP8796093382691http://localhost:9001/ws410/rest/countries/de/regions/DE-RPDE-SL8796093415459http://localhost:9001/ws410/rest/countries/de/regions/DE-SLDE-ST8796093448227http://localhost:9001/ws410/rest/countries/de/regions/DE-STDE-SN8796093480995http://localhost:9001/ws410/rest/countries/de/regions/DE-SNDE-SH8796093513763http://localhost:9001/ws410/rest/countries/de/regions/DE-SHDE-TH8796093546531http://localhost:9001/ws410/rest/countries/de/regions/DE-TH

PS- 当您只添加不带标签的单词时,它们会出现在网络浏览器上。

编辑

I am including my xsl file in my source file like



<?xml-stylesheet type="text/xsl" href="countries2.xsl"?>
    <?xml version="1.0" encoding="ISO-8859-1"?>


          <country isocode="de" pk="8796093055010" uri="http://localhost:9001/ws410/rest/countries/de">
and rest of the xml as source file

但是当我在浏览器 FF 上检查时显示解析错误,而在 IE 上我得到以下输出

DE-BW8796093055011http://localhost:9001/ws410/rest/countries/de/regions/DE-BWDE-BY8796093087779http://localhost:9001/ws410/rest/countries/de/regions/DE-BYDE-BE8796093120547http://localhost:9001/ws410/rest/countries/de/regions/DE-BEDE-BR8796093153315http://localhost:9001/ws410/rest/countries/de/regions/DE-BRDE-HB8796093186083http://localhost:9001/ws410/rest/countries/de/regions/DE-HBDE-HH8796093218851http://localhost:9001/ws410/rest/countries/de/regions/DE-HHDE-HE8796093251619http://localhost:9001/ws410/rest/countries/de/regions/DE-HEDE-MV8796093284387http://localhost:9001/ws410/rest/countries/de/regions/DE-MVDE-NI8796093317155http://localhost:9001/ws410/rest/countries/de/regions/DE-NIDE-NW8796093349923http://localhost:9001/ws410/rest/countries/de/regions/DE-NWDE-RP8796093382691http://localhost:9001/ws410/rest/countries/de/regions/DE-RPDE-SL8796093415459http://localhost:9001/ws410/rest/countries/de/regions/DE-SLDE-ST8796093448227http://localhost:9001/ws410/rest/countries/de/regions/DE-STDE-SN8796093480995http://localhost:9001/ws410/rest/countries/de/regions/DE-SNDE-SH8796093513763http://localhost:9001/ws410/rest/countries/de/regions/DE-SHDE-TH8796093546531http://localhost:9001/ws410/rest/countries/de/regions/DE-TH

IE 上的输出没有标签。我想知道在我的源 xml 中包含 xsl 可以在转换后向我显示正确的输出。

【问题讨论】:

  • 这个问题被标记为 XSLT 2.0。我会尝试将样式表设置为 1.0,众所周知,浏览器不支持 XSLT 2.0。
  • 我要尝试的第一件事是在 XML 源文件中切换 &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;?xml-stylesheet type="text/xsl" href="countries2.xsl"?&gt; 的顺序。 &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; 应该始终排在第一位。

标签: xml xslt


【解决方案1】:

问题不在于您的 XSLT 代码,而在于您运行(或者更确切地说,不运行)转换的方式。我并不完全清楚您是如何尝试执行此操作的,但基本上 XSLT 从未执行过,因此您看到的是源文档的原始文本内容。

【讨论】:

    【解决方案2】:

    这是一个运行的 XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <xsl:apply-templates select="/country/regions"/>
      </xsl:template>
      <xsl:template match="regions">
        <RECORDS>
          <xsl:for-each select="region">
            <RECORD>
              <PROP NAME="isocode">
                <PVAL>
                  <xsl:value-of select="@isocode"/>
                </PVAL>
              </PROP>
              <PROP NAME="pk">
                <PVAL>
                  <xsl:value-of select="@pk"/>
                </PVAL>
              </PROP>
              <PROP NAME="uri">
                <PVAL>
                  <xsl:value-of select="@uri"/>
                </PVAL>
              </PROP>
            </RECORD>
          </xsl:for-each>
        </RECORDS>
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 请不要在样式表中使用 version="1.1"。 1.1 是一个工作草案,从未被推荐。
    • 就IE9而言,我相信它就是它的工作方式。您的最后一次编辑清楚地显示 XSLT 已执行 - 否则,输出中的第一个文本应该是 2011-08-03T21:53:35.624+05:30 而不是 DE-BW8796093055011。这只是IE9如何显示的问题。我在 IE 9 中使用了开发人员工具,在观察结果的同时切换兼容模式肯定有助于理解发生了什么。我没有 Firefox,但我稍后会研究。
    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 2019-12-05
    • 2019-04-11
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多