gpt4 book ai didi

xml - 是否可以从XPath值列表和XML XSD构建XML文档?

转载 作者:行者123 更新时间:2023-12-03 16:21:57 25 4
gpt4 key购买 nike

我有一个要映射到XML文档中的名称/值对列表。我的想法是这样,为每个名称分配一个XPath,如下所示:

Account_Number       = 4294587576-32      = /my:myFields/my:Customer/my:AccountNumber
Customer_Name = John Smith = /my:myFields/my:Customer/my:Name
Customer_Address = Tampa, FL 33604 = /my:myFields/my:Customer/my:Address
Amount_Due = 129.85 = /my:myFields/my:AmountDue/my:Amount
Days_Past_Due = 54 = /my:myFields/my:AmountDue/my:DaysPastDue


现在,我不应该能够将这些信息与XML文档或XSD的完整样本一起使用,并构建看起来像这样的XML文档:

<my:myFields>
<my:Customer>
<my:Name>John Smith</my:Name>
<my:AccountNumber>4294587576-32</my:AccountNumber>
<my:Address>Tampa, FL 33604</my:Address>
</my:Customer>
<my:AmountDue>
<my:DaysPastDue>54</my:DaysPastDue>
<my:Amount>129.85</my:Amount>
</my:AmountDue>
</my:myFields>


我的问题特定于Microsoft InfoPath,因为我需要获取名称/值对的列表并为InfoPath表单构建XML数据文档。执行翻译的技术不必是Microsoft。 Java或C ++将是最佳解决方案。可以使用Apache的Xalan等XSLT处理器来完成此操作吗?

最佳答案

这是一个建议,就像您提到的Java和XSLT一样,我不会为Xalan和XSLT 1.0所困扰,而是使用Saxon 9和XSLT 2.0。假设您有一个格式正确的XML输入样本,并且您在上面的值到XPath表达式的映射中,我将把映射提供给XSLT 2.0样式表,该样式表将生成第二个样式表,然后可以将其应用于输入样本。因此,假设我们将文件“ test2011101201.txt”作为

Account_Number       = 4294587576-32      = /my:myFields/my:Customer/my:AccountNumber
Customer_Name = John Smith = /my:myFields/my:Customer/my:Name
Customer_Address = Tampa, FL 33604 = /my:myFields/my:Customer/my:Address
Amount_Due = 129.85 = /my:myFields/my:AmountDue/my:Amount
Days_Past_Due = 54 = /my:myFields/my:AmountDue/my:DaysPastDue


样式表

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:axsl="http://www.w3.org/1999/XSL/Transforma"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://example.com/my"
version="2.0"
exclude-result-prefixes="xs">

<xsl:param name="text-file" as="xs:string" select="'test2011101201.txt'"/>
<xsl:variable name="lines" as="xs:string*" select="tokenize(unparsed-text($text-file), '\r?\n')[normalize-space()]"/>

<xsl:output method="xml" indent="yes"/>

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

<xsl:template match="/">
<axsl:stylesheet version="2.0">
<axsl:template match="@* | node()">
<axsl:copy>
<axsl:apply-templates select="@*, node()"/>
</axsl:copy>
</axsl:template>
<xsl:for-each select="$lines">
<xsl:variable name="tokens" select="tokenize(., '=')"/>
<axsl:template match="{normalize-space($tokens[3])}">
<axsl:copy>
<axsl:apply-templates select="@*"/>
<axsl:text>
<xsl:value-of select="replace($tokens[2], '(^\s+|\s+$)', '')"/>
</axsl:text>
</axsl:copy>
</axsl:template>
</xsl:for-each>
</axsl:stylesheet>
</xsl:template>

</xsl:stylesheet>


可以运行以生成第二个样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://example.com/my"
version="2.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/my:myFields/my:Customer/my:AccountNumber">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>4294587576-32</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="/my:myFields/my:Customer/my:Name">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>John Smith</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="/my:myFields/my:Customer/my:Address">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>Tampa, FL 33604</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="/my:myFields/my:AmountDue/my:Amount">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>129.85</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="/my:myFields/my:AmountDue/my:DaysPastDue">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>54</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


当应用于输入样本时

<my:myFields xmlns:my="http://example.com/my">
<my:Customer>
<my:Name></my:Name>
<my:AccountNumber></my:AccountNumber>
<my:Address></my:Address>
</my:Customer>
<my:AmountDue>
<my:DaysPastDue></my:DaysPastDue>
<my:Amount></my:Amount>
</my:AmountDue>
</my:myFields>


输出

<?xml version="1.0" encoding="UTF-8"?><my:myFields xmlns:my="http://example.com/my">
<my:Customer>
<my:Name>John Smith</my:Name>
<my:AccountNumber>4294587576-32</my:AccountNumber>
<my:Address>Tampa, FL 33604</my:Address>
</my:Customer>
<my:AmountDue>
<my:DaysPastDue>54</my:DaysPastDue>
<my:Amount>129.85</my:Amount>
</my:AmountDue>
</my:myFields>


就像我说的那样,这需要XSLT 2.0和像Saxon 9这样的XSLT 2.0处理器来解析文本文件。无论文本文件的作者是谁,都需要确保样式表映射到XSLT匹配模式的路径表达式不明确。

关于xml - 是否可以从XPath值列表和XML XSD构建XML文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742687/

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