XML 문서의 속성 값과 위치에 따라 출력 문서의 특정 위치에 XML 노드를 추가해야합니다.xslt 속성 값을 기준으로 n 번째 형제를 가져옵니다.
아래 예제에서는 product = "111"이 나타날 때마다 product = "222"인 하위 항목에 해당하는 경로 속성을 가져와야합니다. 주의 사항은 문서의 상대 위치가 일치해야한다는 것입니다. 첫 번째 제품 = "111"의 경우, 첫 번째 제품의 경로 = "222"가 필요합니다. 두 번째 "111"의 경우 두 번째 제품인 "222"의 경로가 필요합니다.
출력 내가 (I는 또한 현재 제품의 경로를 추가 할 수 있지만 그와 아무 문제가 없음)가 필요합니다
<output>
<product_out id="111">
<path>b</path>
</product_out>
<product_out id="111">
<path>g</path>
</product_out>
<product_out id="111">
<path>i</path>
</product_out>
</output>
내 XML 문서.
<?xml version="1.0"?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<item product="111">
<sub_item path="a" />
</item>
<item product="222">
<sub_item path="b" />
</item>
<item product="333">
<sub_item path="c" />
</item>
<item product="111">
<sub_item path="d" />
</item>
<item product="111">
<sub_item path="e" />
</item>
<item product="555">
<sub_item path="f" />
</item>
<item product="222">
<sub_item path="g" />
</item>
<item product="555">
<sub_item path="h" />
</item>
<item product="222">
<sub_item path="i" />
</item>
</order>
내 XSL 문서 :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<output>
<xsl:apply-templates />
</output>
</xsl:template>
<xsl:template match="order/item">
<product_out>
<xsl:attribute name="id"><xsl:value-of select="@product"/></xsl:attribute>
<xsl:if test="@product = 111">
<the_correct_sibling_position><xsl:value-of select="1+count(preceding-sibling::item[@product='111'])"/></the_correct_sibling_position>
<path_test1><xsl:value-of select="/order/item[position()=4]/sub_item/@path"/></path_test1>
<path_test2><xsl:value-of select="/order/item[2]/sub_item/@path"/></path_test2>
<path_test3><xsl:value-of select="/order/item[position()=number(1+count(preceding-sibling::item[@product='111']))]/sub_item/@path"/></path_test3>
<path_test4><xsl:value-of select="/order/item[position()=1+count(preceding-sibling::item[@product='111'])]/sub_item/@path"/></path_test4>
</xsl:if>
<path><xsl:value-of select="sub_item/@path[1]"/></path>
</product_out>
</xsl:template>
</xsl:stylesheet>
은 내가
1 + 카운트 (이전-형제를 사용하여 필요한 정확한 배열 위치를 얻을 수 있습니다 :: 항목 [제품 @ = '111'])
나는 출력 <the_correct_sibling_position> 노드 내에서 값을 지정하십시오.
I는 하드 코딩 노드에 위치 할 수
path_test1 always returns d
path_test2 always returns b
다음 2 개 테스트하지 출력 값 B는, g, 난 등 I 기대할 것이다.
path_test3 always returns a
path_test4 always returns a
어떻게 B, g를 반환하는 경로를 어떻게해야합니까, 나는 제품 노드 "111"와 "222"의 위치에 따라? 앞선 형제 변수는 내 select 구문의 경로 안에 예상했던 것처럼 작동하지 않습니다.
도와주세요!
스택 오버 플로우 (나는 6 시간을 더 기다릴 필요가) 날 수 있습니다 때 나는 내 질문에 대답합니다. 나는 이전의 시도 중 일부를 재시험했는데 지금은 효과가있다. 변수를 추가 한 다음 select 문에서 사용했습니다. 가변 코드 : 내 선택 진술 : path_test_good> –
user1513023