【发布时间】:2012-10-26 07:23:48
【问题描述】:
我在使用 XSLT 将 xml 文件转换为 xml 时遇到问题。
我似乎可以获得第一个节点的属性,但是我无法获得名为 <attribute> 的节点及其属性和值,在某些情况下,节点 <attribute> 有一个名为 <codedvalue> 的节点,它具有属性和我需要的值:
这是一个文件示例(编辑:为便于阅读添加了空格):
<?xml version='1.0' encoding='UTF-8'?>
<arb:result xmlns:arb="urn::codeservice">
<arb:document
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn::codeservice prod_code.xsd">
<header>Produced by CodeRows 4.2</header>
<body>
<termsystem id="1" begindate="1970-01-01T00:00:01"
expirationdate="2099-12-31T23:59:59"
lastmodifieddate="2012-10-05T09:52:02.35522"
lastmodifiedby="Admin">
<attribute type="longname" datatype="ST"
language="fi">1</attribute>
<attribute type="status" datatype="ST">1</attribute>
<attribute type="codetype" datatype="ST">1</attribute>
<attribute type="relatesto" datatype="ST">BASE</attribute>
<attribute type="relatestoname" datatype="ST">BASE</attribute>
<attribute type="hierarchical" datatype="ST">0</attribute>
<termitementry id="1010110" language="fi"
createdate="2007-10-25T15:24:17.0"
begindate="2003-01-01T00:00:01.0"
expirationdate="2004-12-31T23:59:59.0"
lastmodifieddate="2007-10-25T17:06:25.0"
lastmodifiedby="Admin">
<attribute type="status" datatype="ST">1</attribute>
<attribute type="shortname" datatype="ST" language="fi">Whey protein</attribute>
<attribute type="longname" datatype="ST" language="fi" />
<attribute type="abbreviation" datatype="ST"
language="fi">Raspberry</attribute>
<attribute type="hierarchylevel" datatype="ST">0</attribute>
<attribute type="parentid" datatype="ST" language="fi" />
<attribute type="description" datatype="ST" language="fi" />
<attribute type="owner" datatype="ST"
language="sv">Admin</attribute>
<attribute type="externallink"
datatype="CV"
begindate="2003-01-01T00:00:01.0"
expirationdate="2005-12-31T23:59:59.0"
><codedvalue code="08"
codesystem="PUN"
codesystemversion="1"
referenceid="BASEpowder" /></attribute>
<attribute type="externallink"
datatype="CV"
begindate="2001-01-01T00:00:01.0"
expirationdate="2006-12-31T23:59:59.0"
><codedvalue code="80112"
codesystem="PROTEIN"
codesystemversion="1"
referenceid="BASEprotein" /></attribute>
<attribute type="externallink"
datatype="CV"
begindate="2003-01-01T00:00:01.0"
expirationdate="2008-01-31T23:59:59.0"
><codedvalue code="03"
codesystem="REF"
codesystemversion="1"
referenceid="BASEref" /></attribute>
</termitementry
></termsystem
></body
></arb:document
></arb:result>
我使用的 XSLT 是:
<?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"/>
<xsl:template match="termitementry">
<xsl:element name="CodeRow">
<xsl:element name="Code">
<xsl:apply-templates select="@id" />
</xsl:element>
<xsl:element name="Begindate">
<xsl:apply-templates select="@begindate" />
</xsl:element>
<xsl:element name="Expirationdate">
<xsl:apply-templates select="@expirationdate" />
</xsl:element>
<xsl:element name="Lastmodifiedby">
<xsl:apply-templates select="@lastmodifiedby" />
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="@id">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="@begindate">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="@expirationdate">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="@lastmodifiedby">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
有了这个,我似乎只得到了第一个节点 <termitementry> 的属性,但在 + 之后没有任何东西,我得到了一些废话,我正在使用 http://chris.photobooks.com/xml/default.htm 来测试一下
<transformiix:result>
Produced by CodeRows 4.2
1
1
1
BASE
BASE
0
<CodeRow>
<Code>
1010110
</Code>
<Begindate>
2003-01-01T00:00:01.0
</Begindate>
<Expirationdate>
2004-12-31T23:59:59.0
</Expirationdate>
<Lastmodifiedby>
Admin
</Lastmodifiedby>
</CodeRow>
</transformiix:result>
有人可以帮忙解决剩下的问题吗?
我想得到一个基本结构如下的xml:
<coderow>
<Code>
1010110
</Code>
<Begindate>
2003-01-01 (only want the 10 first digits of 2003-01-01T00:00:01.0, preferably I would just to have the digits so it looks like 20030101)
</Begindate>
<Shortname>
Whey protein
</Shortname>
<BASEpowder>
08
<BASEpowder>
<BASEprotein>
80112
</BASEprotein>
</CodeRow>
我怎么会跳出“由 CodeRows 4.2 等生产。”却看不到我为它制作了任何模板?
【问题讨论】:
-
问题不清楚,请用输入xml中的实际值更新输出。