2

我正在尝试使用 XSLT 1 将层次结构/结构展平为 XML,但没有成功。- 甚至找到好的链接...

输入xml


<Address addressType="R">
 <Structured xmlns="cds_dt">
  <Line1>15 Paradise</Line1>
  <City>Toronto</City>
  <CountrySubdivisionCode>-50</CountrySubdivisionCode>
  <PostalZipCode>
    <PostalCode>A1A1O1</PostalCode>
  </PostalZipCode>
 </Structured>
</Address>

所需的输出 xml


<Address addressType="R">
  <Formatted xmlns="cds_dt">15 Paradise, Toronto, A1A1O1</Formatted>
</Address>

我试过这个 .xsl 但没有运气 - 文件中的错误


<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:x="cds"> 

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*[ancestor::address]">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()[ancestor::address::Structured]">
  <xsl:value-of select="concat('&#44;',.)"/>
</xsl:template>

</xsl:stylesheet> 
4

2 回答 2

3

这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="cds_dt">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="x:Structured">
  <xsl:element name="Formatted" namespace="cds_dt">
   <xsl:value-of select=
   "concat(x:Line1, ', ', x:City, ', ', x:PostalZipCode/x:PostalCode)"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Address addressType="R">
    <Structured xmlns="cds_dt">
        <Line1>15 Paradise</Line1>
        <City>Toronto</City>
        <CountrySubdivisionCode>-50</CountrySubdivisionCode>
        <PostalZipCode>
            <PostalCode>A1A1O1</PostalCode>
        </PostalZipCode>
    </Structured>
</Address>

产生想要的正确结果

<Address addressType="R">
   <Formatted xmlns="cds_dt">15 Paradise, Toronto, A1A1O1</Formatted>
</Address>

解释:覆盖身份规则+ 正确使用命名空间和<xsl:element>指令。

于 2012-03-02T21:15:06.207 回答
0

你的意思是这样的:

<Address addressType="R">
    <Formatted xmlns="cds_dt">
        <xsl:value-of select="concat(Line1, ', ', City, PostalZipCode/PostalCode )"/>
    </Formatted>
</Address>

注意:为了便于阅读,我缩短了 concat() 的参数路径。所以Line1实际上应该是而不是Address/Structured/Line1

于 2012-03-02T21:04:06.730 回答