Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
如何从另一个xml生成xml属性值而不使用xslt硬编码_Xml_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

如何从另一个xml生成xml属性值而不使用xslt硬编码

如何从另一个xml生成xml属性值而不使用xslt硬编码,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,我看到了一个使用JSTL将xml转换为另一个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</crea

我看到了一个使用JSTL将xml转换为另一个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>
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BY</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093087779</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BY</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BE</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093120547</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BE</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BR</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093153315</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BR</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HB</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093186083</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HB</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093218851</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HH</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HE</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093251619</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HE</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-MV</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093284387</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-MV</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-NI</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093317155</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-NI</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-NW</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093349923</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-NW</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-RP</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093382691</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-RP</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SL</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093415459</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SL</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-ST</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093448227</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-ST</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SN</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093480995</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SN</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093513763</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SH</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-TH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093546531</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-TH</PVAL>
        </PROP>
    </RECORD>
</RECORDS>
country isocode=“de”pk=“8796093055010”uri=”http://localhost:9001/ws410/rest/countries/de">
2011-08-03T21:53:35.624+05:30
2011-08-03T22:05:10.111+05:30
真的
德国
XSLT

<?xml version="1.0" encoding="UTF-8"?>
<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>

输出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>
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BY</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093087779</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BY</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BE</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093120547</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BE</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-BR</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093153315</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BR</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HB</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093186083</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HB</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093218851</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HH</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-HE</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093251619</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-HE</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-MV</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093284387</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-MV</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-NI</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093317155</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-NI</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-NW</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093349923</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-NW</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-RP</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093382691</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-RP</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SL</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093415459</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SL</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-ST</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093448227</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-ST</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SN</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093480995</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SN</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-SH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093513763</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-SH</PVAL>
        </PROP>
    </RECORD>
    <RECORD>
        <PROP name="isocode">
            <PVAL>DE-TH</PVAL>
        </PROP>
        <PROP name="pk">
            <PVAL>8796093546531</PVAL>
        </PROP>
        <PROP name="uri">
            <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-TH</PVAL>
        </PROP>
    </RECORD>
</RECORDS>

DE-BW
8796093055011
http://localhost:9001/ws410/rest/countries/de/regions/DE-体重
德比
8796093087779
http://localhost:9001/ws410/rest/countries/de/regions/DE-借
德贝
8796093120547
http://localhost:9001/ws410/rest/countries/de/regions/DE-是
去溴
8796093153315
http://localhost:9001/ws410/rest/countries/de/regions/DE-溴
去血红蛋白
8796093186083
http://localhost:9001/ws410/rest/countries/de/regions/DE-血红蛋白
DE-HH
8796093218851
http://localhost:9001/ws410/rest/countries/de/regions/DE-嗯
德赫
8796093251619
http://localhost:9001/ws410/rest/countries/de/regions/DE-他
DE-MV
8796093284387
http://localhost:9001/ws410/rest/countries/de/regions/DE-MV
德尼
8796093317155
http://localhost:9001/ws410/rest/countries/de/regions/DE-倪
西北方向
8796093349923
http://localhost:9001/ws410/rest/countries/de/regions/DE-西北
DE-RP
8796093382691
http://localhost:9001/ws410/rest/countries/de/regions/DE-反相
DE-SL
8796093415459
http://localhost:9001/ws410/rest/countries/de/regions/DE-SL
德圣
8796093448227
http://localhost:9001/ws410/rest/countries/de/regions/DE-圣
脱锡
8796093480995
http://localhost:9001/ws410/rest/countries/de/regions/DE-锡
德什
8796093513763
http://localhost:9001/ws410/rest/countries/de/regions/DE-嘘
德思
8796093546531
http://localhost:9001/ws410/rest/countries/de/regions/DE-TH

在这个输出xml中,我创建了许多记录子元素。我的问题是我想在不进行硬编码的情况下转换PROB name属性()值。我需要从输入xml中读取reagion元素属性,并将该值转换为输出xml。请帮我找到解决办法
谢谢……

我相信这就是您想要的:

<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:apply-templates select="region" />
    </RECORDS>
  </xsl:template>

  <xsl:template match="region">
    <RECORD>
      <xsl:apply-templates select="@*" />
    </RECORD>
  </xsl:template>

  <xsl:template match="region/@*">
    <PROP name="{name()}">
      <PVAL>
        <xsl:value-of select="." />
      </PVAL>
    </PROP>
  </xsl:template>
</xsl:stylesheet>

在输入XML上运行时,将生成以下输出:

<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>
  <RECORD>
    <PROP name="isocode">
      <PVAL>DE-BY</PVAL>
    </PROP>
    <PROP name="pk">
      <PVAL>8796093087779</PVAL>
    </PROP>
    <PROP name="uri">
      <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BY</PVAL>
    </PROP>
  </RECORD>
  <!-- (Several more similar records) -->
</RECORDS>

DE-BW
8796093055011
http://localhost:9001/ws410/rest/countries/de/regions/DE-体重
德比
8796093087779
http://localhost:9001/ws410/rest/countries/de/regions/DE-借

是。这就是我想做的。非常感谢。@user1586908太好了。不客气。如果有效,请记住通过单击旁边的复选标记图标接受答案。谢谢