1

我有这个 xml 数据

<XMLCreators>
<row>
<RateNumber>1</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Abesinia Passage</AssessmentStreet>
<RateAccomDesc>Dwelling  (Part Of)</RateAccomDesc>
</row>
<row>
<RateNumber>1a</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Arena's Palace Lane</AssessmentStreet>
<RateAccomDesc>Edmund's Home</RateAccomDesc>
</row>
</XMLCreators>

我用这个 xsl 改变了它的输出元素

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" method="xml"/> 
<xsl:template match="/">
    <Address>  <xsl:apply-templates/> </Address>
    </xsl:template>
<xsl:template match="/XMLCreators/row">
    <Rowinfo> <xsl:apply-templates/> </Rowinfo>
</xsl:template>

<xsl:template match="/XMLCreators/row/RateNumber">
    <Locator> <xsl:apply-templates/> </Locator>
</xsl:template>
<xsl:template match="/XMLCreators/row/AssessmentStreet">
    <thoroughfare> <xsl:apply-templates/> </thoroughfare>
</xsl:template>

<xsl:template match="/XMLCreators/row/RateAccomDesc">
    <LocatorName> <xsl:apply-templates/> </LocatorName>
</xsl:template>

     </xsl:stylesheet>

并制作了这个

<?xml version="1.0" encoding="utf-8"?>
<Address>
<Rowinfo>
<Locator>1</Locator>
Null
<thoroughfare>Abesinia Passage</thoroughfare>
<LocatorName>Dwelling  (Part Of)</LocatorName>
</Rowinfo>
<Rowinfo>
<Locator>1a</Locator>
Null
<thoroughfare>Arena's Palace Lane</thoroughfare>
<LocatorName>Edmund's Home</LocatorName>
</Rowinfo>
</Address>

我想向 rowinfo (locatordesignator) 添加一个新元素,并使用 (rateletter 和 RateAccomDesc) 的值的串联填充它。

拜托,我需要这方面的帮助。

4

2 回答 2

1

只需添加此模板

 <xsl:template match="RateLetter">
  <LocatorDesignator>
   <xsl:value-of select="concat(., ', ', ../RateAccomDesc)"/>
  </LocatorDesignator>
 </xsl:template>

现在完整的转换变成:

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

 <xsl:template match="/*">
  <Address>  <xsl:apply-templates/> </Address>
 </xsl:template>

 <xsl:template match="row">
  <Rowinfo> <xsl:apply-templates/> </Rowinfo>
 </xsl:template>

 <xsl:template match="RateNumber">
  <Locator> <xsl:apply-templates/> </Locator>
 </xsl:template>

 <xsl:template match="AssessmentStreet">
  <thoroughfare> <xsl:apply-templates/> </thoroughfare>
 </xsl:template>

 <xsl:template match="RateAccomDesc">
  <LocatorName> <xsl:apply-templates/> </LocatorName>
 </xsl:template>

 <xsl:template match="RateLetter">
  <LocatorDesignator>
   <xsl:value-of select="concat(., ', ', ../RateAccomDesc)"/>
  </LocatorDesignator>
 </xsl:template>
</xsl:stylesheet>

当它应用于提供的 XML 文档时:

<XMLCreators>
    <row>
        <RateNumber>1</RateNumber>
        <RateLetter>Null</RateLetter>
        <AssessmentStreet>Abesinia Passage</AssessmentStreet>
        <RateAccomDesc>Dwelling  (Part Of)</RateAccomDesc>
    </row>
    <row>
        <RateNumber>1a</RateNumber>
        <RateLetter>Null</RateLetter>
        <AssessmentStreet>Arena's Palace Lane</AssessmentStreet>
        <RateAccomDesc>Edmund's Home</RateAccomDesc>
    </row>
</XMLCreators>

产生了想要的正确结果

<Address>
   <Rowinfo>
      <Locator>1</Locator>
      <LocatorDesignator>Null, Dwelling  (Part Of)</LocatorDesignator>
      <thoroughfare>Abesinia Passage</thoroughfare>
      <LocatorName>Dwelling  (Part Of)</LocatorName>
   </Rowinfo>
   <Rowinfo>
      <Locator>1a</Locator>
      <LocatorDesignator>Null, Edmund's Home</LocatorDesignator>
      <thoroughfare>Arena's Palace Lane</thoroughfare>
      <LocatorName>Edmund's Home</LocatorName>
   </Rowinfo>
</Address>

说明

正确使用模板和标准 XPathconcat()函数。

于 2012-08-19T21:42:50.673 回答
0

只需将附加信息添加到您的模板之一:

<xsl:template match="/XMLCreators/row">
    <Rowinfo>
        <locatordesignator>
            <xsl:value-of select="RateLetter"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="RateAccomDesc"/>
        </locatordesignator>

        <xsl:apply-templates/>
    </Rowinfo>
</xsl:template>
于 2012-08-19T21:12:13.007 回答