gpt4 book ai didi

xslt - "Expected QName"- 尝试从属性值创建元素

转载 作者:行者123 更新时间:2023-12-04 16:52:05 25 4
gpt4 key购买 nike

尝试使用 XSL 样式表转换简单的 XML 文档。我现在正在使用 XMLspy,但最终目标是浏览器。

XML:

<doc>


<str name="organizationName">Me</str>
<str name="rights">BY-NC</str>
<str name="date">2011-05-23</str>
<str name="type">Collection</str>
<bool name="collectionPubliclyVisible">true</bool>
<str name="publisher">Pub</str>
<str name="creator">Me</str>
<long name="id">2656</long>
<int name="rank">2</int>
<str name="contributor">ME</str>
<str name="description">This Collection archives 900+ feeds from the network of US based NOAA observation stations recording current climatic conditions, in addition to a daily constructed XML Zip file generated by NOAA.</str>
<str name2="name">NOAA - XML Feeds of Observed Current Conditions</str>
<date name="updated_dt">2011-06-03T21:04:56Z</date>
<str name="relation"/>
<str name="format">zip</str>
<date name="created_dt">2011-05-31T22:36:07Z</date>
<date name="timestamp">2011-06-17T21:54:24.116Z</date>
</doc>

XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:element name="metadata">
<xsl:apply-templates select="doc"/>
</xsl:element>
</xsl:template>

<xsl:template match="doc">
<xsl:apply-templates select="child::node()"/>
</xsl:template>

<xsl:template match="child::node()">
<element name="{@name}" xmlns="http://www.w3.org/1999/XSL/Transform">
<xsl:value-of select="."/>
</element>
</xsl:template>


</xsl:stylesheet>

非常感谢,我一直在这几个小时无济于事。

-格雷厄姆

最佳答案

您正在尝试使用 name 创建输出元素属性:

<element name="{@name}" xmlns="http://www.w3.org/1999/XSL/Transform">

但是查看您的输入 XML 文档,有一个元素是 name2属性,这会导致您的转换出错。
<str name2="name">NOAA - XML Feeds of Observed Current Conditions</str>

我不确定您期望输出什么 XML,但是您可以尝试使用此 XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/">
<metadata>
<xsl:apply-templates select="doc/*"/>
</metadata>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{@*}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

结果 XML:
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<organizationName>Me</organizationName>
<rights>BY-NC</rights>
<date>2011-05-23</date>
<type>Collection</type>
<collectionPubliclyVisible>true</collectionPubliclyVisible>
<publisher>Pub</publisher>
<creator>Me</creator>
<id>2656</id>
<rank>2</rank>
<contributor>ME</contributor>
<description>This Collection archives 900+ feeds from the network of US based NOAA
observation stations recording current climatic conditions, in addition to a daily
constructed XML Zip file generated by NOAA.</description>
<name>NOAA - XML Feeds of Observed Current Conditions</name>
<updated_dt>2011-06-03T21:04:56Z</updated_dt>
<relation/>
<format>zip</format>
<created_dt>2011-05-31T22:36:07Z</created_dt>
<timestamp>2011-06-17T21:54:24.116Z</timestamp>
</metadata>

关于xslt - "Expected QName"- 尝试从属性值创建元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com