how to reference a subset of child nodes whose element names are number

Im trying to transform an XML doc where there is an address node that has child elements Address[1-9]

currently, Im using the following xslt to concatenate all the lines into a single output

out:AddressLine
<xsl:value-of select=”$nodeOfAddress/*[starts-with(name(), ‘in:AddressLine’)]/text()[normalize-space()]/normalize-space()” separator=” “/>
</out:AddressLine>

This seems to work great for ALL address lines. However, I only want to do this for lines 3-6, not all 9 of them. Is there a way specify that in the for each select? Im sure I could hack out a way with a few if statements, but was hoping maybe there is a way similar to what I have above.

I was asked for a sample in file. I looks something like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><in:AddressNode>
<in:AddressLine1>Value1</in:AddressLine1>
<in:AddressLine2>Value1</in:AddressLine2>
<in:AddressLine3>Value1</in:AddressLine3>
<in:AddressLine4>Value1</in:AddressLine4>
<in:AddressLine5>Value1</in:AddressLine5>
<in:AddressLine6>Value1</in:AddressLine6>
<in:AddressLine7>Value1</in:AddressLine7>
<in:AddressLine8>Value1</in:AddressLine8>
<in:AddressLine9>Value1</in:AddressLine9>
</in:AddressNode>
</code>
<code><in:AddressNode> <in:AddressLine1>Value1</in:AddressLine1> <in:AddressLine2>Value1</in:AddressLine2> <in:AddressLine3>Value1</in:AddressLine3> <in:AddressLine4>Value1</in:AddressLine4> <in:AddressLine5>Value1</in:AddressLine5> <in:AddressLine6>Value1</in:AddressLine6> <in:AddressLine7>Value1</in:AddressLine7> <in:AddressLine8>Value1</in:AddressLine8> <in:AddressLine9>Value1</in:AddressLine9> </in:AddressNode> </code>
<in:AddressNode>
    <in:AddressLine1>Value1</in:AddressLine1>
    <in:AddressLine2>Value1</in:AddressLine2>
    <in:AddressLine3>Value1</in:AddressLine3>
    <in:AddressLine4>Value1</in:AddressLine4>
    <in:AddressLine5>Value1</in:AddressLine5>
    <in:AddressLine6>Value1</in:AddressLine6>
    <in:AddressLine7>Value1</in:AddressLine7>
    <in:AddressLine8>Value1</in:AddressLine8>
    <in:AddressLine9>Value1</in:AddressLine9>
</in:AddressNode>

I just want address3, 4, 5, and 6

5

Maybe:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><xsl:value-of select="$NodeOfAddress/*[starts-with(name(), 'in:AddressLine')][3 le position() and position() le 6]" />
</code>
<code><xsl:value-of select="$NodeOfAddress/*[starts-with(name(), 'in:AddressLine')][3 le position() and position() le 6]" /> </code>
<xsl:value-of select="$NodeOfAddress/*[starts-with(name(), 'in:AddressLine')][3 le position() and position() le 6]" /> 

Hard to be sure without seeing the input.

0

You can use the sequence (3, 4, 5, 6) (which I’ve written below in the equivalent form (3 to 6)) in your predicate to select the AddressLine child elements by position() e.g.:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:out="out"
xmlns:in="in">
<xsl:template match="/">
<xsl:variable name="nodeOfAddressLine" select="in:AddressNode/*"/>
<out:AddressLine><xsl:value-of
select="$nodeOfAddressLine[position()=(3 to 6)]"
separator=" "
/></out:AddressLine>
</xsl:template>
</xsl:stylesheet>
</code>
<code><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:out="out" xmlns:in="in"> <xsl:template match="/"> <xsl:variable name="nodeOfAddressLine" select="in:AddressNode/*"/> <out:AddressLine><xsl:value-of select="$nodeOfAddressLine[position()=(3 to 6)]" separator=" " /></out:AddressLine> </xsl:template> </xsl:stylesheet> </code>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:out="out"
  xmlns:in="in">
  <xsl:template match="/">
    <xsl:variable name="nodeOfAddressLine" select="in:AddressNode/*"/>
    <out:AddressLine><xsl:value-of 
      select="$nodeOfAddressLine[position()=(3 to 6)]" 
      separator=" "
    /></out:AddressLine>
  </xsl:template>
</xsl:stylesheet>

That’s assuming that your sample input is really representative, and that the only children of AddressNode are the AddressLine elements you want. But if that the AddressNode element does have other children, then you’d need to select only the ones whose names start with AddressLine, e.g.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <xsl:variable name="nodeOfAddressLine"
select="in:AddressNode/*[starts-with(local-name(), 'AddressLine')]"/>
</code>
<code> <xsl:variable name="nodeOfAddressLine" select="in:AddressNode/*[starts-with(local-name(), 'AddressLine')]"/> </code>
    <xsl:variable name="nodeOfAddressLine" 
       select="in:AddressNode/*[starts-with(local-name(), 'AddressLine')]"/>

Personally I would probably just explicitly select the various AddressLine elements by name, if there are only 4 different elements, as I think it makes the intent clearer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:out="out"
xmlns:in="in">
<xsl:template match="/">
<out:AddressLine><xsl:value-of
select="in:AddressNode/
(in:AddressLine3 | in:AddressLine4 | in:AddressLine5 | in:AddressLine6)"
separator=" "
/></out:AddressLine>
</xsl:template>
</xsl:stylesheet>
</code>
<code><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:out="out" xmlns:in="in"> <xsl:template match="/"> <out:AddressLine><xsl:value-of select="in:AddressNode/ (in:AddressLine3 | in:AddressLine4 | in:AddressLine5 | in:AddressLine6)" separator=" " /></out:AddressLine> </xsl:template> </xsl:stylesheet> </code>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:out="out"
  xmlns:in="in">
  <xsl:template match="/">
    <out:AddressLine><xsl:value-of 
      select="in:AddressNode/
         (in:AddressLine3 | in:AddressLine4 | in:AddressLine5 | in:AddressLine6)"
      separator=" "
    /></out:AddressLine>
  </xsl:template>
</xsl:stylesheet>

If the prefix: in in your xml and/or the positions of your child-elements can vary, I would use this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><xsl:value-of select="$NodeOfAddress/*[number(substring-after(name(),'AddressLine')) = (3 to 6)]" />
</code>
<code><xsl:value-of select="$NodeOfAddress/*[number(substring-after(name(),'AddressLine')) = (3 to 6)]" /> </code>
<xsl:value-of select="$NodeOfAddress/*[number(substring-after(name(),'AddressLine')) = (3 to 6)]" />   

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật